Skip to content

Instantly share code, notes, and snippets.

@betamos
Created August 23, 2013 15:29
Show Gist options
  • Save betamos/6320628 to your computer and use it in GitHub Desktop.
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.
/* Author: Didrik Nordström, didrik@betamos.se */
/**
* 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