How to callback each loop after completion of ajax call.
Please find my code as follows.
Story:
Let us assume I have 3 values X,Y,Z. Firstly, I am taking X value, sending to django views and their using requests module for getting some info and pushing to the div class push_new_info_here, in the next Iteration I have to take Y value. How to do ? please note: Previous ajax call should be succeeded .
Final word: I am collecting all the info of (X,Y,Z), then merging using python and pushing to the div class push_new_info_here
window.onload = function() {
$.each(["X","Y","Z"], function( index, value ) {
$.post('/find/'+value+'/',{},function(data){
$('.push_new_info_here').empty();
$('.push_new_info_here').html(data);
});
});
};
Like this - use .append if you want to see the data or change the timeout to something long enough for the user to see:
var arr = ["X","Y","Z"],cnt=0;
function postIt() {
if (cnt >= arr.length) return; // stop
$.post('/find/'+arr[cnt]+'/',{},function(data){
$('.push_new_info_here').empty().html(data); // or .append
cnt++;
postIt(); // or setTimeout(postIt,3000); // give server a breather
});
}
$(function() {
postIt();
});
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments