I do not like any answer at all, the best way (since Jquery 1.5+) is to use Deferred objects, those are objects to manipulate async calls, you can solve :
You can read more about it in the jquery official documentation:
$.when($.ajax("/page1.php"), $.ajax("/page2.php")) .then(myFunc, myFailure);
This way myFunc executes after the 2 ajax calls are made, and myFailure if either one has an error.You can read more about it in the jquery official documentation:
JQuery Deferred Object
First off, you might want to consider doing the startup code in a single call.
Second: Instead of waiting just call another function call. for the above code it should look something like:
Second: Instead of waiting just call another function call. for the above code it should look something like:
for (j=1; j <= 7; j++){
(function(index) {
$.getJSON('my.php', {id:index},
function(data) {
$.each(data, function(index2, array){
........
});
if (j === 7) {
initDoneDoMoreStuff()
}
});
})(j)
}
or trigger:
for (j=1; j <= 7; j++){
(function(index) {
$.getJSON('my.php', {id:index},
function(data) {
$.each(data, function(index2, array){
........
});
if (j === 7) {
$(document).trigger("initdone");
}
});
})(j)
}
$(document).bind("initdone", function() {....});
Read full article from javascript - jquery : wait until all ajax calls finish then continue - Stack Overflow
No comments:
Post a Comment