4-5次Ajax调用成功后调用javascript函数

Calling javascript function on success of 4-5 Ajax Calls

本文关键字:调用 javascript 函数 成功 Ajax 4-5次      更新时间:2023-09-26

我需要使4-5 Ajax调用,一旦所有这些调用是成功的,我需要调用一个JavaScript函数。我正在使用ExtJS。我也可以通过使用JQuery进行这些Ajax调用。

一旦所有这些Ajax调用都成功,我如何调用函数?

我可以创建4-5个旗帜&在每个ajax调用成功时,我将各自的标志设置为true &一旦所有标志都为真,我将进行函数调用。

有更好的方法来达到预期的结果吗?

您可以使用jQuery when:

$.when(
    // Get first
    $.get("/first/", function(result) {
        //Do something with result
    }),
    // Get second
    $.get("/second/", function(result) {
        //Do something with result
    }),
    // Get third
    $.get("/third/", function(result) {
        //Do something with result
    })
).then(function() {
    // All is ready now, so...
});

来源:http://css-tricks.com/multiple-simultaneous-ajax-requests-one-callback-jquery/

在jquery网站上也有记录:http://api.jquery.com/jquery.when/