G
G
gsdev992019-08-30 09:11:27
React
gsdev99, 2019-08-30 09:11:27

How to properly clear the interval?

Hello. I have a counter implementation. When you press the "start" button, the stopwatch should start and after a specified time interval increase its value by the value of the interval. When you click on "stop", the stopwatch should stop and reset its value
The problem is the following, please tell me how to reset setInterval correctly

handleStop = () => {
  this.setState({ currentTime: 0 })
}

so that after calling this method, the state does not again fall into setInterval
https://codepen.io/gsdev99/pen/gOYRVBr

Answer the question

In order to leave comments, you need to log in

2 answer(s)
0
0xD34F, 2019-08-30
@gsdev99

What, you haven't heard about clearInterval ?

I
irishmann, 2019-08-30
@irishmann

Before learning React, it is imperative that you first learn VanillaJS. In the documentation forThe setInterval explicitly states that the setInterval identifier must be passed to clearInterval , which is also written in the answers to the question at the link you provided in the comment to the previous answer. No need to reinvent the wheel, take and use what you have.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question