Skip to content

Instantly share code, notes, and snippets.

@jperler
Created February 17, 2015 18:10
Show Gist options
  • Save jperler/77b0c5d93fc7b4097918 to your computer and use it in GitHub Desktop.
Save jperler/77b0c5d93fc7b4097918 to your computer and use it in GitHub Desktop.
Simulates a setInterval call which calls different functions at each interval.
var Interval = function(interval) {
this.interval = interval;
this.calls = 0;
this.funcs = [];
};
Interval.prototype.push = function(func, context) {
this.funcs.push(func.bind(context || window));
};
Interval.prototype.call = function() {
var func;
while ((func = this.funcs.shift())) {
setTimeout(func, this.interval * this.calls);
this.calls += 1;
}
};
// tests
var interval = new Interval(1000);
var logFunc = function(i) {
return function() { console.log('interval: ' + i); };
};
for (var i = 0; i < 10; i += 1) {
interval.push(logFunc(i));
}
interval.call();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment