Answer the question
In order to leave comments, you need to log in
How to track the completion of all ajax requests on the page?
Actually, the task is this, you need to run N requests to the server in a cycle, after all requests have been completed, you will need to run 1 more control one. How to track the completion of all ajax requests on the page?
Now my code looks like this:
jQuery('.btn-primary').on('click', function(event) {
event.preventDefault();
for (var i = 0; i <= jQuery('#count').val(); i++)
{
var form = jQuery('.form-horizontal').serializeArray();
jQuery.ajax({
url: 'myurl',
type: 'POST',
data: {form, i},
success:function(data)
{
jQuery('.cool').append(data);
}
});
}
});
Answer the question
In order to leave comments, you need to log in
Yes, of course, you can look at the value of the external counter at a certain interval ...
But you can do it right right away :
var form = {foo: 'bar'},
requests = [],
index,
$xhr;
for (var index = 0; index <= 5; index++) {
$xhr = $.post('myurl', {'form': form, 'index': index});
$xhr.done(function (data) {
console.log('request done');
});
requests.push($xhr);
}
$.when.apply($, requests).done(function () {
console.log('all done');
})
Do it with the ajaxStop method :
$( document ).ajaxStop(function() {
$('.cool').append(data);
});
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question