0

I have the following code that sets the interval at which another function (a 1 minute timer) is executed. I'd like to be able to change this interval elsewhere to control play, pause and reset buttons. I was thinking of starting the program with an interval of 0 so the method is not yet called, changing to 1000 when play is pressed, and then toggling between 0 and 1000 when a pause button is pressed once and then pressed again. Is there a straight forward way to do this?

componentDidMount() {
  
    setInterval(this.countdownMinute, 1000);
    }
john
  • 17
  • 4
  • Does this answer your question? [Changing the interval of SetInterval while it's running](https://stackoverflow.com/questions/1280263/changing-the-interval-of-setinterval-while-its-running) – Kevin Wang Jul 06 '20 at 18:12

1 Answers1

2

You can stop the interval by clearInterval function. For example:

this.intervalId = setInterval(this.countdownMinute, 1000);

clearInterval(this.intervalId);
  • I'd like to be able to pause a countdown, then restart it. I don't know how to call setinterval or clearinterval from code outside of componentdidmount. I'd like to be able to set the millisecond argument in setinterval from other methods. So that when user clicks pause milliseconds is set to 0, then back to 1000 when they click pause again. – john Jul 06 '20 at 21:38