Callback Queue Function
by amindunited
JavaScript
$(function(){
//SOF betshopQueue
function betshopQueue (callBack) {
var qArray = [];//An array of items to be queued, or q'd if you are spelling lazily
var running = false;//Boolean to mark while the queue is being proccessed
var callBack = callBack;
var parent = this;
return function (newItem) {
function proccessQueue () {
running = true;//Flag this function as running
//If there are items in the queue ... proccess them
if (qArray.length > 0) {
//Make sure that we have a function to apply, before we try to apply it
if (typeof callBack === "function") {
callBack.apply(parent, [qArray[0], taskComplete]);
}
} else {
running = false;
console.log("The queue is now empty ");
}
};
function taskComplete () {
console.log("taskComplete", qArray[0], qArray);
qArray.shift();//Must remove this item from the queue array
proccessQueue();//run this again **to avoid having a 'last lap' put this in if (qArray.length > 0)
}
//if an item has been passed to the function...add it to the queue
if (newItem) {
qArray.push(newItem);
//if the queue isn't currently being proccessed...start it
if (running === false) {
proccessQueue();
}
}
}
}//EOF betshopQueue
/*
* Notice that in each callback function there are 2 arguments
* ...The first argument is the data that you want to act on
* ...The second argument is a function...it can have any name...but you MUST call it when your task is complete!
*/
//SOF First queue Example
var...