The fourth is the same as the third except there's now two workers that are both working to empty the queue meaning there are two requests going at any time until the queue is empty and once the final request is complete, it triggers a deferred
var urls = [];
//Make a bunch of URLs, use delay= to make them take time to load.
for (var i = 0; i < 10; i++) {
var n = Math.floor(Math.random() * 5);
urls.push("http://jsfiddle.net/echo/jsonp/?delay=" + n + "&data=" + i);
}
var index = 0;
function worker(id) {
var alldone = $.Deferred();
//Do the next piece of work to be done.
//This can be starting a new AJAX request or triggering the
//final "alldone" deferred.
function next() {
if (index >= urls.length) {
log(id + ": Finished my work");
alldone.resolve();
return;
}
var i = index++;
log(id + ": Starting number " + i);
$.ajax({
url: urls[i],
dataType: "jsonp",
success: function() {
log(id + ": Finished number " + i);
}
}).done(next);
}
//Start the first one
next();
return alldone;
}
$.when(worker("A"), worker("B")).done(function() {
log("Last one finished!");
});
function log(msg) {
$("<div>", {
text: msg
}).appendTo("body");
}
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.