Skip to content

Instantly share code, notes, and snippets.

@issm
Created December 29, 2011 07:21
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 issm/1532557 to your computer and use it in GitHub Desktop.
Save issm/1532557 to your computer and use it in GitHub Desktop.
jobsequencer.js
window.JobSequencer = function () { return this.init.apply(this, arguments); };
JobSequencer.prototype = {
init: function (params) {
params = params || {};
var self = this;
self._queue = [];
self._stopped = false;
self._count = 0;
self.interval = params.interval || 0;
return self;
},
push: function (f, sec) {
this._queue.push({ f: f, s: sec || this.interval });
},
shift: function () {
return this._queue.shift();
},
start: function () {
this._stopped = false;
return this._exec();
},
stop: function () {
this._stopped = true;
},
_exec: function () {
var self = this;
var f_this = arguments.callee;
if ( self._stopped ) { return }
var o = self.shift();
if ( typeof o !== 'undefined' ) {
var params = {
id: ++self._count
};
setTimeout(function () {
o.f(params);
f_this.call(self, []);
}, o.s);
}
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment