setInterval keeps speeding up

101
January 23, 2020, at 8:50 PM

New to Stack and new, more or less, to JS and React (trying to go through a free coding bootcamp).

Anyways, I'm working on doing a Pomodoro clock and am using setInterval for the countdown.

Hitting the play/pause button changes the state and starts the countdown timer. Hitting it again, pauses it (pausing works just fine). But starting the clock UP again, it considerably speeds up, as if by hitting the play button, it changes the interval in setInterval, almost to the point where it counts down by one as normal then speeds up, counts down as normal, then speeds up again. I'm not sure where it's going wrong or how this is being caused. Any help is appreciated.

 startStop(){
    this.changeState();
    this.startClock();
  }
  changeState(){
    if (this.state.timer_Running == true){
      this.setState({timer_Running: false})
    } else {
      this.setState({timer_Running: true})
    }
  }
  startClock() {
    this.setState({timer_State: setInterval(this.timerFunc, 1000)}); //Sets the interval at 1000ms or 1 second
  }
  timerFunc() { 
    if (this.state.timer_Running == true) { //Timer running is a state set to false so that when the play/pause button is hit, it switches to true and then the timer then begins.
      if (this.state.timer_Sec == 0) {
        this.setState({
          timer_Sec: 10, 
          timer_Min: this.state.timer_Min - 1
        });
      }
      if (this.state.timer_Sec > 0) {
        this.setState({timer_Sec: this.state.timer_Sec - 1})
      }
      if (this.state.timer_Min <= 0 && this.state.timer_Sec == 0) {
        //Need to change this to make sure that it then adds in break to replace session and start that timer.
        this.stopClock();  
      }
    } else{
      this.stopClock();
    }
  }

  stopClock() {
    clearInterval(this.state.timer_State);
  }
READ ALSO
Send file from server to client (browser) through socket io using socket.io-stream node package

Send file from server to client (browser) through socket io using socket.io-stream node package

I am trying to receive a file from the socket io server using socketio-stream package

154
Trying to find a property recursively in JavaScript

Trying to find a property recursively in JavaScript

I have an object and want to traverse recursively until I get to my property and value I wish

91
Element slides up and down repeatedly on scroll

Element slides up and down repeatedly on scroll

I am creating a scroll to top button that appears when a user has scrolled to a certain pointI created a CodePen that works, but my regular code doesn't; the button slides up and down repeatedly

113
Google script time out

Google script time out

My script is timing out, I guess because it's going over the 6 minute limitDoes anyone have an idea how I can optimize it so it runs faster?

111