I've looked through many similar questions but the proposed solutions do not work in all cases as expected. The following code works fine when all ajax calls are successfully done but if any of ajax calls happens to fail, then onComplete
is immediately called:
var deferredArray = $('.preload').map(function() {
return $.get(this.href)
});
$.when.apply($, deferredArray).then(onComplete, onComplete);
So there can be two cases:
- all deferred calls are successful, then
onComplete
is called afterwards - works fine; - some deferred call fails (returns HTTP 400 Bad request), then
onComplete
is called immediately not waiting for other deferred calls.
The second case represents my problem. It should always wait for all calls to complete regardless of the status before calling onComplete
.
I use jQuery 1.7.1 as it's built into the framework. If the issue is due to the version, I can upgrade but I'd prefer to keep the current version.
You could try
ajaxStop()
. Allbeit not the most elegant solution.Also note:
If
$.ajax()
or$.ajaxSetup()
is called with the global option set to false, the.ajaxStop()
method will not fire.