Wait until all jQuery Ajax requests are done?
How do I make a function wait until all jQuery Ajax requests are done inside another function?
In short, I need to wait for all Ajax requests to be done before I execute the next. But how?
jQuery now defines a when function for this purpose.
It accepts any number of Deferred objects as arguments, and executes a function when all of them resolve.
That means, if you want to initiate (for example) four ajax requests, then perform an action when they are done, you could do something like this:
$.when(ajax1(), ajax2(), ajax3(), ajax4()).done(function(a1, a2, a3, a4){
// the code here will be executed when all four ajax requests resolve.
// a1, a2, a3 and a4 are lists of length 3 containing the response text,
// status, and jqXHR object for each of the four ajax calls respectively.
});
function ajax1() {
// NOTE: This function must return the value
// from calling the $.ajax() method.
return $.ajax({
url: "someUrl",
dataType: "json",
data: yourJsonData,
...
});
}
In my opinion, it makes for a clean and clear syntax, and avoids involving any global variables such as ajaxStart and ajaxStop, which could have unwanted side effects as your page develops.
If you don't know in advance how many ajax arguments you need to wait for (ie you want to use a variable number of arguments), it can still be done but is just a little bit trickier. See Pass in an array of Deferreds to $.when() (and maybe jQuery .when troubleshooting with variable number of arguments).
If you need deeper control over the failure modes of the ajax scripts etc., you can save the object returned by .when()
- it's a jQuery Promise object encompassing all of the original ajax queries. You can call .then()
or .fail()
on it to add detailed success/failure handlers.
If you want to wait until all ajax requests are finished in your document, no matter how many of them exist, just use $.ajaxStop
event this way:
$(document).ajaxStop(function () {
// 0 === $.active
});
In this case, there is no need to guess how many requests can be in an application that might finish in the future. In some cases ajax requests can be part of a function's inner logic, which can be quite complicated (eg calling other functions), and in that case, you might not wait until said function is done with its entire logic rather than only waiting for the ajax
part to complete.
$.ajaxStop
here can also be bound to any HTML
node that you think might be modified by ajax
.
Again the purpose of this handler is to know when there is no active ajax
not to clear or reset something.
PS If you don't mind using ES6 syntax, then you can use Promise.all
for known ajax
methods. Example:
Promise.all([ajax1(), ajax2()]).then(() => {
// all requests finished successfully
}).catch(() => {
// all requests finished but one or more failed
})
An interesting point here is that works both with Promises
and $.ajax
requests. Here is jsFiddle demonstrating the last one.
I found a good answer by gnarf my self which is exactly what I was looking for :)
jQuery ajaxQueue
//This handles the queues
(function($) {
var ajaxQueue = $({});
$.ajaxQueue = function(ajaxOpts) {
var oldComplete = ajaxOpts.complete;
ajaxQueue.queue(function(next) {
ajaxOpts.complete = function() {
if (oldComplete) oldComplete.apply(this, arguments);
next();
};
$.ajax(ajaxOpts);
});
};
})(jQuery);
Then you can add a ajax request to the queue like this:
$.ajaxQueue({
url: 'page.php',
data: {id: 1},
type: 'POST',
success: function(data) {
$('#status').html(data);
}
});
链接地址: http://www.djcxy.com/p/9510.html
上一篇: 复选框值始终为'on'
下一篇: 等到所有jQuery Ajax请求完成?