So I have a 'for' that on each iteration calls a different ajax call, in the callback of each call I collect some urls and run an ajax call for each url.
It looks something like this
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() is used to have a delay of 100ms between calls to get_info(), get info is a a function containing an async ajax call.
I do them async to try to not overload the server with requests, and because I tried to find an order so I could know when it finishes, but it doesn't work.
As it's async jquery's ajaxStop triggers after every single ajax is done.
Not sure how to do this with promises either.
Help is appreciated.
EDIT: Here's how get_info looks like:
function get_info(url, page_num)
{
$.ajax({ url: url, async: false, success: function(data)
{
// this is optional, sometimes it's true sometimes 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');
}
}
});
}