Skip to content

Instantly share code, notes, and snippets.

@kaitwalla
Created September 11, 2014 19:09
Show Gist options
  • Save kaitwalla/b767246e1dd129b94d3f to your computer and use it in GitHub Desktop.
Save kaitwalla/b767246e1dd129b94d3f to your computer and use it in GitHub Desktop.
Dequeuer for push in Javascript
function dequeue(data) {
if (temp) {
queue.push(data);
var time = temp.getTimeLeft();
temp.stop();
temp = new timer(function () {
temp = false;
dequeue();
},time);
}
else if (queue.length == 0) {
// Fired on push w/ no queue
clearInterval(mainInterval);
doThings(workingVar);
//set temp as a timer instead of just the interval because
temp = new timer(function() {
mainFunction();
},11000);
}
else {
workingVar = queue[0];
queue.splice(0,1);
doThings(workingVar);
if (queue.length > 0) {
temp = new timer(function () {
dequeue();
},11000);
}
else {
mainInterval = setInterval(function() { mainFunction(); },11000);
temp = false;
}
}
}
doThings(data) {
//This is the function that does your action with the pushed data
}
mainFunction() {
//This function is what sets things into motion when the queue is not employed. Probably called on page load the first time
mainInterval = setInterval(function() { //stuff },11000);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment