gpt4 book ai didi

javascript - 如何确保我的函数在所有 ajax 调用后执行?

转载 作者:行者123 更新时间:2023-11-28 15:56:24 25 4
gpt4 key购买 nike

我有一个 javascript 函数。在这个函数内我有 3 个 ajax 调用。我如何确保我的另一个函数在这些 ajax 调用完成后执行?

function a(){
for(var i=0;i<3;i++)
$.post("somePage.php").done(function(){
do stuff here...
});
return false;
}
function b(){
//.....
}

ajax 调用完成后我的函数 b 会执行吗?

最佳答案

完整的解决方案类似于

function a() {
var array = [], xhr;
for (var i = 0; i < 3; i++) {
xhr = $.post("somePage.php").done(function () {});
array.push(xhr)
}
//create a promise which will be called after all the ajax request are completed
return $.when.apply($, array);
}

function b() {
//.....
}

//on succss callback of all the ajax requests created in a call b
a().done(b)

关于javascript - 如何确保我的函数在所有 ajax 调用后执行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18642291/

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