Created
February 20, 2014 10:16
-
-
Save igorkulman/9110555 to your computer and use it in GitHub Desktop.
Request queue for JavaScript
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function requestQueue(url, startCallback, doneCallback) { | |
var requests = []; | |
function createRequest(parameters) { | |
var f = function() { | |
$.post(url, parameters) | |
.done(function() { | |
if (requests.length > 0) { | |
//console.log("processing next request"); | |
var next = requests.pop(); | |
next(); | |
} else { | |
//console.log("done"); | |
doneCallback(); | |
} | |
}); | |
}; | |
return f; | |
} | |
function enqueue(parameters) { | |
requests.push(createRequest(parameters)); | |
} | |
function start() { | |
var first = requests.pop(); | |
if (first == undefined) { | |
//console.log("empty queue"); | |
} else { | |
//console.log("starting"); | |
startCallback(); | |
first(); | |
} | |
} | |
return { | |
start: start, | |
enqueue: enqueue | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
So every time you call requestQueue, what type of function would startCallback, doneCallback be? Can you provide an example to give some context as to how you call it? Thanks...
is it something like this perhaps?
var finished = false;
var p1 = requestQueue(url. p1.start, p2)
var p2 = requestQueue(url, p2.start, completed)
function completed() (
finished = true;
)