I have a web page that can submit an unlimited number of items back to the server for processing.
I've decided to submit these items in groups of 25 using AJAX calls to a web service. So my loop looks something like this:
// Submit elements
for (var i = 0; i < ids.length; i += 25) {
var productIds = ids.slice(i, Math.min(i + 25, ids.length - 1));
$.post('/Services/ImportProducts.asmx/ImportProducts', JSON.stringify({ importProductIds: productIds }))
.done(function (result, statusText, jqxhr) {
// TODO: Update progress
})
.always(function () {
// TODO: Test for error handling here
});
}
So far, this seems right. However, when all processing is complete, I want to refresh the page. And given the code above, I'm not seeing an easy way to perform a task when the last AJAX call has completed.
Since $.post()
is asynchronous, this loop will complete before the AJAX calls have. And since the AJAX calls could complete in a different order than they were submitted, I can't simply test for when my last-submitted call is done.
How do I know when this code is done?