Created
August 23, 2013 15:29
-
-
Save betamos/6320628 to your computer and use it in GitHub Desktop.
JS tool for scheduling a function for execution multiple times at a specific 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
/* Author: Didrik Nordström, [email protected] */ | |
/** | |
* Schedule a function for execution multiple times at a specific interval. | |
* Uses setTimeout so remember to not use it for high precision timing over | |
* long durations. | |
* | |
* @param timeout Duration in ms from now to first call | |
* @param count Number of calls | |
* @param interval Duration in ms between calls | |
* @param fn(i) Function to call | |
* i Index of the call, between 0 and count-1 | |
*/ | |
function burstTimeout(timeout, count, interval, fn) { | |
for (var i = 0; i < count; i++) { | |
(function(i) { // Self invoking fn to preserve i in scope | |
setTimeout(function() { | |
fn(i); | |
}, timeout + i * interval); | |
})(i); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment