Created
February 17, 2015 18:10
-
-
Save jperler/77b0c5d93fc7b4097918 to your computer and use it in GitHub Desktop.
Simulates a setInterval call which calls different functions at each interval.
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
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