I am in need of creating a sequence of AJAX requests to different URLs, and only after all of them have completed I need to initiate another set of calls. This is the code I have written so far:
(function (delay) {
var tasksToDo = 10,
currentTask = 0;
var process = setInterval(function() {
// make a call
// blahblahblah
// then:
if(currentTask === tasksToDo) {
clearInterval(process);
} else {
currentTask++;
}
}, delay);
})(2000);
// The same pattern repeats for the second series, but it will execute immediately due to being asynchronous.
The reason I have structured my code like this instead of using a simple for
loop is because I require a pause between each request. The second set of calls cannot begin until the first set is complete, as the URLs needed for the second set are obtained from the completion of the first set; therefore, I cannot merge all the calls into the same self-calling function. It is crucial for the second series to start executing only after the first series has finished.