Recursive Setinterval() Runs Continuously
I'm trying to run a function every 5 seconds using JavaScript using a recursive setInterval function. The following code just logs 'started' as fast as possible and then crashes t
Solution 1:
You're callingfive
immediately, instead of merely passing it in:
functionfive () {
console.log("five");
}
setInterval(five, 5000);
/* ^ */
Solution 2:
Change this line:
setInterval(five(), 5000);
like this:
setInterval(five, 5000);
But seems like what you really need is:
setTimeout(five, 5000);
So your code will look like:
function five() {
console.log("five");
setTimeout(five, 5000);
}
five();
Solution 3:
The reason of crashing it is that you are calling the function five. Instead of that you should pass it as parameter.
setInterval(five, 5000);
Post a Comment for "Recursive Setinterval() Runs Continuously"