Skip to content

Instantly share code, notes, and snippets.

@issm issm/jobsequencer.js
Created Dec 29, 2011

Embed
What would you like to do?
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
You can’t perform that action at this time.