I’m having trouble with the countdown timer.
It stops when I click the play/pause button, stops at time zero, but for some reason counts down way too fast, increasingly fast as it continues to count down.
I set the interval to every 1000 milliseconds, so I’m not entirely sure what the problem is.
Here are the relevant parts of the code:
countdown(){
if(this.state.timerunning===true && this.state.timeleft > 0){
this.setState(prevState => {
return{
timeleft: prevState.timeleft - 1
}
})
}else{
clearInterval(this.go);
}
}
The below toggles if the clock runs or not and is triggered by a button click:
Running(){
this.setState(prevState => {
return{
timerunning: !prevState.timerunning
}})
}
This is the componentWillUpdate where the interval is set with the countdown function, and componentWillUnmount to clear the interval
componentWillUpdate(){
this.go = setInterval(this.countdown.bind(this),1000);
}
componentWillUnmount(){
clearInterval(this.go);
}
Link to entire project: https://codepen.io/yisun1/pen/jOmMZmv?editors=1111