Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/429.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何确保我的函数在所有ajax调用之后执行?_Javascript_Jquery_Html - Fatal编程技术网

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

Javascript 如何确保我的函数在所有ajax调用之后执行?,javascript,jquery,html,Javascript,Jquery,Html,我有一个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(){ //..... } 函数a(){ 对于(va

我有一个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(){
    //.....
}
函数a(){

对于(var i=0;i编写一个增量检查,在每个返回的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...
});

编写一个增量检查,对每个返回的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...
});

一个完整的解决方案将类似于

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)
函数a(){
var数组=[],xhr;
对于(变量i=0;i<3;i++){
xhr=$.post(“somePage.php”).done(函数(){});
array.push(xhr)
}
//创建一个承诺,该承诺将在所有ajax请求完成后调用
返回$.when.apply($,数组);
}
函数b(){
//.....
}
//成功回调调用中创建的所有ajax请求b
a().完成(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)
函数a(){
var数组=[],xhr;
对于(变量i=0;i<3;i++){
xhr=$.post(“somePage.php”).done(函数(){});
array.push(xhr)
}
//创建一个承诺,该承诺将在所有ajax请求完成后调用
返回$.when.apply($,数组);
}
函数b(){
//.....
}
//成功回调调用中创建的所有ajax请求b
a().完成(b)

jQuery方法允许您在所有Ajax请求完成时注册一个要调用的处理程序。

jQuery方法允许您在所有Ajax请求完成时注册一个要调用的处理程序。

否…..如何调用b?如何调用no的可能重复项…..如何调用b?如何调用只有潜在i的可能重复项假设,取决于调用此函数的方式,它过去返回false,但现在不返回。唯一的潜在问题,取决于调用此函数的方式,是它过去返回false,但现在不返回。