İlker
İlker

Reputation: 2090

clearInterval is not working properly on a progress bar

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

Answers (1)

Shubham Kumar
Shubham Kumar

Reputation: 596

This should work fine:

const clear = setInterval(() => 
      setProgress((prev) => {
        if (prev >= 99) {
          clearInterval(clear);
        }
        return prev + 1
      }), 10);

Upvotes: 1

Related Questions