Skip to content

Instantly share code, notes, and snippets.

@fosron
Created August 6, 2014 07:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fosron/5262dbfa3f139e8f9728 to your computer and use it in GitHub Desktop.
Save fosron/5262dbfa3f139e8f9728 to your computer and use it in GitHub Desktop.
sequencer.js
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();
});
//
// 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