gpt4 book ai didi

javascript - 来自多个 ajax 请求的求和值

转载 作者:行者123 更新时间:2023-11-30 20:54:47 25 4
gpt4 key购买 nike

我正在尝试获取 ajax 请求返回的数据的总值,但它显示的是 total:0,因为它在完成 ajax 请求之前正在执行 totalRev。

var totalRev = 0;
$.ajax({
type: "POST",
url: "cloudmobi.php",
data: {action: 'cloudmobi'},
dataType:'JSON',
success: function(response){
document.getElementById('cloudmobi').innerHTML = response.cloudmobi;
console.log(response.cloudmobi);
var cloudmobi = parseInt(response.cloudmobi);
console.log('CLOUDMOBI:'+cloudmobi);
totalRev += cloudmobi;

}
});
$.ajax({
type: "POST",
url: "mobusi.php",
data: {action: 'mobusi'},
dataType:'JSON',
success: function(response){
document.getElementById('mobusi').innerHTML = response.mobusi;
console.log(response.mobusi);
var mobusi = parseInt(response.mobusi);
totalRev += mobusi;
console.log('MOBUSI:'+mobusi);

}
});
$.ajax({
type: "POST",
url: "appnext.php",
data: {action: 'appnext'},
dataType:'JSON',
success: function(response){
document.getElementById('appnext').innerHTML = response.appnext;
console.log(response.appnext);
var appnext = parseInt(response.appnext);
totalRev += appnext;
console.log('APPNEXT:'+appnext);

}
});
console.log('TOTAL:'+totalRev);

我不想使用异步,因为在这里使用 ajax 的全部目的是快速加载站点然后动态加载数据

最佳答案

jQuery“when”解决了您的问题:

$.when( d1, d2 ).done(function ( v1, v2 ) {
console.log( v1 ); // "Fish"
console.log( v2 ); // "Pizza"
});

关于javascript - 来自多个 ajax 请求的求和值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47781716/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com