Reputation: 2090
I'm stuck on a very basic problem which I want to clear the interval when my progress is 100, but it doesn't stop.
const clear = setInterval(() => {
setProgress((prev) => prev + 1);
if (progress > 99) {
clearInterval(clear);
}
}, 10);
I can't see my mistake here.
Upvotes: 0
Views: 307
Reputation: 596
This should work fine:
const clear = setInterval(() =>
setProgress((prev) => {
if (prev >= 99) {
clearInterval(clear);
}
return prev + 1
}), 10);
Upvotes: 1