javascript - Series of async ajax calls inside a series of async ajax calls. How to know when all calls are finished? -
so have 'for' on each iteration calls different ajax call, in callback of each call collect urls , run ajax call each url.
it looks
for(var i=1; i<num_pages + 1; i++) { var page_url = main_url + "&sf=" + i; console.log('=== doing page: ' + page_url); try { $.ajax({url: page_url, async:false, success: function(data) { var urls = []; $('a', data).each(function() { if($(this).attr('itemprop') == 'url') { var u = $(this).attr('href'); if(urls.indexof(u) == -1) { urls.push(u); } } }); // for(var j=0; j<urls.length; j++) for(var j=0; j<3; j++) { dosettimeout(j, i); } } }); } catch(err) { } } function dosettimeout(i, page_num) { settimeout(function() { get_info(urls[i], page_num); }, 100); }
dosettimeout() used have delay of 100ms between calls get_info(), info a function containing async ajax call.
i them async try not overload server requests, , because tried find order know when finishes, doesn't work.
as it's async jquery's ajaxstop triggers after every single ajax done.
not sure how promises either.
help appreciated.
edit: here's how get_info looks like:
function get_info(url, page_num) { $.ajax({ url: url, async: false, success: function(data) { // optional, it's true it's false if(views_enabled) { $.ajax({type:'post', async:false, url:'/someurl/', data:{price: price}, success: function() { item['views'] = data.views; all_data.push(item); console.log('finished item'); } }); } else { console.log('finished item'); } } }); }
Comments
Post a Comment