Created
August 6, 2014 07:44
-
-
Save fosron/5262dbfa3f139e8f9728 to your computer and use it in GitHub Desktop.
sequencer.js
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
shiv.addOnLoad(function() { | |
var seq = new Sequencer([ | |
function(cb) { | |
shiv.log("Log callback 1"); | |
cb && cb(); | |
}, | |
function(cb) { | |
shiv.log("Log callback 2"); | |
cb && cb(); | |
}, | |
function(cb) { | |
shiv.log("Log callback 3"); | |
cb && cb(); | |
} | |
]); | |
seq.start(); | |
}); |
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
// | |
// sequencer.js - sequencer implementation | |
// | |
Sequencer = function(arr, opts) { | |
arr = arr || []; | |
var running = false; | |
var self = this; | |
this.isRunning = function() { | |
return running; | |
}; | |
this.start = function(cb) { | |
cb && self.add(cb); | |
running = true; | |
this.next(); | |
}; | |
this.stop = function() { | |
running = false; | |
}; | |
this.startCallback = function(cb) { | |
return function() { | |
self.next(); | |
}; | |
}; | |
this.add = function(cb) { | |
arr.push(cb); | |
}; | |
this.next = function() { | |
if (running && (arr.length != 0)) { | |
arr.shift()(function() { | |
self.next(); | |
}); | |
} | |
}; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment