Last active
October 9, 2017 20:56
-
-
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.
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
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