You can create a "Master Deferred", which will only resolve when all of the other Deferreds (the AJAX requests) have completed successfully;
jQuery.when(jQuery.get('/foo'), jQuery.post('/bar'), jQuery.get('/baz')).done(function () {
$('button').show();
});
The syntax is to pass each Deferred
as a parameter to jQuery.when()
, which returns a Deferred
which resolves when one fails, or when all of them complete.
If you don't know beforehand how many AJAX requests you have, already have them in an array, or just don't want to use the above, you can use Function.apply
like so;
var ajaxRequests = [jQuery.get('/foo'), jQuery.post('/bar'), jQuery.get('/baz')];
jQuery.when.apply(jQuery, ajaxRequests).done(function () {
$('button').show();
});
For more info, see http://api.jquery.com/jQuery.when, or http://www.mattlunn.me.uk/blog/2014/01/tracking-joining-parallel-ajax-requests-with-jquery/ (my blog)