如何检查所有Ajax是否已完成

时间:2014-05-19 11:07:17

标签: php ajax

我在页面上使用倍数ajax

1. $.load("abc.com/users");
2. $.load("abc.com/usersrates"); 
3. ..... 
4. .......

并且有一个下一个按钮可以转到另一个禁用的页面现在我想在所有Ajax完成后启用此下一个按钮。

任何想法都会受到赞赏。

3 个答案:

答案 0 :(得分:1)

使用$.when进行多次ajax调用。

示例:

 $.when( $.load("abc.com/users"), $.load("abc.com/usersrates"))
      .then( successCallback, errorCallback );

有关详细信息,请参阅此jQuery.when()

答案 1 :(得分:0)

编辑:此处的$.when响应要好得多!

这有点hacky但会起作用

<script>
var totalLoads = 2;
var currentLoad = 0;

function checkLoads()
{
    if (currentLoad == totalLoads)
    {
        // enable button
    }
}

$.load("abc.com/users", funciton() { currentLoad++; checkLoads(); });
$.load("abc.com/usersrates", funciton() { currentLoad++; checkLoads(); })); 
</script>

答案 2 :(得分:0)

使用$.when

    $.when( $.ajax(...), $.ajax( ... ) ).done(function( a1, a2 ) {



      }
    }).failed(function(f1,f2){ 


  });
  

//每个参数都是一个具有以下结构的数组:[data,   statusText,jqXHR]

     

// a1 a2 是针对第一个和第二个ajax解析的参数   成功请求    // f1 f2 是参数   分别解决了第一个和第二个ajax失败的请求。

相关问题