我有一个javascript function.Inside这个函数,我有3个ajax调用。如何确保我的另一个函数在这些ajax调用完成后执行?
function a(){
for(var i=0;i<3;i++)
$.post("somePage.php").done(function(){
do stuff here...
});
return false;
}
function b(){
//.....
}
我的函数b会在ajax调用完成后执行吗?
发布于 2013-09-06 01:22:20
一个完整的解决方案应该是这样的
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)
发布于 2013-09-06 01:20:44
编写一个增量检查,它对每个返回的Ajax进行增量检查,如下所示:
increments = 0;
$.post("somePage.php").done(function(){
// Check if the increments are total to 3
++increments;
if(increments == 3) {
//do something now such as call b();
}
//do stuff here...
});
发布于 2014-02-21 05:21:49
Ajax ajaxStop方法允许您注册一个处理程序,该处理程序将在所有Ajax请求完成时调用。
https://stackoverflow.com/questions/18642291
复制相似问题