Skip to content

Instantly share code, notes, and snippets.

@jackrugile
Last active October 9, 2017 20:56
Show Gist options
  • Save jackrugile/2797a0fdc5e9bbac29b80fdb3fb609cb to your computer and use it in GitHub Desktop.
Save jackrugile/2797a0fdc5e9bbac29b80fdb3fb609cb to your computer and use it in GitHub Desktop.
A helper function that is the same as setInterval(), but with a limit parameter.
function setLimitedInterval(func, delay, limit) {
let i = 0;
let interval = setInterval(() => {
func(i);
if(++i >= limit) clearInterval(interval);
}, delay);
return interval;
}
// Example: console.log() the current iteration number 3 times, every second
setLimitedInterval((i) => {
console.log(i);
}, 1000, 3);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment