I am having trouble with setInterval. When I run the following code, setMilliseconds(milliseconds - 100);
seems only to run once. Say milliseconds
starts at 100, the console log just shows `900' in an infinite loop.
timer = setInterval(() => {
setMilliseconds(milliseconds - 100);
console.log(milliseconds);
if (milliseconds <= 0) {
console.log("stopped");
clearInterval(timer);
pause();
}
}, 100);
setMilliseconds
id defined above the code as const [milliseconds, setMilliseconds] = useState(0);
I almost never use setIterval. I a doing somithing wrong?
Here is the full file: https://gist.github.com/timscodebase/86fd0951a30b150b438a04a3fd5ec4c1