Created
September 16, 2015 05:09
-
-
Save jimanx2/a5ea26c70115b28c890f to your computer and use it in GitHub Desktop.
This file contains hidden or 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
var ScriptQueue = function(){ | |
var $this = this; | |
var cbStack = []; | |
var currentCbIndex = -1; | |
this.cbTimers = {}; | |
this.queue = function(cb){ | |
cbStack.push(cb); | |
return $this; | |
} | |
this.start = function(){ | |
for(var i in cbStack){ | |
if( i == 0 ){ | |
if($this.debug) | |
console.log("[CB-{0}]".format(i),"Time:", new Date().getMilliseconds(), " Starting cb-"+i); | |
$this.cbTimers[i] = 0; | |
cbStack[i](i); | |
} else { | |
$this.cbTimers[i] = wait(i, function(){ | |
cbStack[i](i); | |
}); | |
} | |
} | |
} | |
this.stop = function(){ | |
if( $this.debug ) console.log("Stopping."); | |
for(var i in $this.cbTimers){ | |
clearTimeout($this.cbTimers[i]); | |
} | |
} | |
var wait = function(idx, cb){ | |
return setInterval(function(){ | |
if($this.cbTimers[idx-1] != null) { | |
if($this.debug) | |
console.log("[CB-{0}]".format(idx),"Waiting for CB-"+(idx-1)); | |
return; | |
} | |
clearInterval($this.cbTimers[idx]); | |
if($this.debug) | |
console.log("[CB-{0}]".format(idx),"Time:", new Date().getMilliseconds(), " Starting cb-"+idx); | |
cbStack[idx](idx); | |
}, 200); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment