Created
December 15, 2011 20:10
-
-
Save shripadk/1482647 to your computer and use it in GitHub Desktop.
queue
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
ZQueue = function() { | |
this.queue = []; | |
this.queued = false; | |
}; | |
ZQueue.prototype.dequeue = function() { | |
this.queued = false; | |
this.shiftQueue(); | |
}; | |
ZQueue.prototype.enqueue = function(fn_to_execute) { | |
// no items in queue | |
if(!this.queued) { | |
this.queued = true; | |
fn_to_execute(); | |
} else { | |
// queue item to run later | |
this.queue.push(fn_to_execute); | |
} | |
}; | |
ZQueue.prototype.shiftQueue = function() { | |
if(this.queue.length) { | |
this.queued = true; | |
var fn_to_execute = this.queue.shift(); | |
fn_to_execute(); | |
} | |
}; | |
ZQueue.prototype.flushQueue = function() { | |
var len = this.queue.length; | |
while(len--) { | |
this.dequeue(); | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
for loop without queue:
for loop with queue: