Javascript: best solution to wait for all ajax callbacks to be executed
Posted
by glaz666
on Stack Overflow
See other posts from Stack Overflow
or by glaz666
Published on 2010-05-28T10:27:53Z
Indexed on
2010/05/28
10:31 UTC
Read the original article
Hit count: 173
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!
© Stack Overflow or respective owner