Hello mighty people of the internet :)
When playing around a bit with some JS I noticed that I was unable to use the return value of setInterval(function, milliseconds)
inside clearInterval(var)
.
Here is a working version of clearing an interval:
const intervalId = setInterval(() => foo(), 500);
setTimeout(() => clearInterval(intervalId), 2000);
In this example an interval is calling the method foo()
every 500ms. After 2s this will be stopped by the clearInterval(intervalId)
nested in the setTimeout(function, milliseconds)
.
Not working is using setInterval inside clearInterval:
setTimeout(() => clearInterval(setInterval(() => foo(), 500)), 2000);
- (Note: Replacing the arrow function syntax for a normal function call didn't change results)
Different to the example above, here no variable is used to hold the return value of setInterval()
.
Yet it results in the interval never being called / immediately stopped.
This appears very strange to me (and potentially could be a bug?).
May somebody can give an opinion / explain this?
Thank you :)