Best solution to wait for all ajax callbacks to be executed
- by glaz666
Hi!
Imagine we have to sources to be requested by ajax. I want to perform some actions when all callbacks are triggered. How this can be done besides this approach:
(function($){
var sources = ['http://source1.com', 'http://source2.com'],
guard = 0,
someHandler = function() {
if (guard != sources.length) { return; }
//do some actions
};
for (var idx in sources) {
$.getJSON(sources[idx], function(){ guard++; someHandler(); })
}
})(jQuery)
What I don't like here is that in this case I can't handle response failing (eg. I can't set timeout for response to come) and overall approach (I suppose there should be a way to use more power of functional programming here)
Any ideas?
Regards!