0

I am adding a timer in my react app. Currently it keeps counting seconds even after 60 seconds, I want it to reset seconds to 0 and minutes ++ obviously.

Here is my code, any tips for how I can achieve this?

 componentDidMount() {
this.myInterval = setInterval(() => {
  this.setState(({ seconds, minutes }) => ({
    seconds: seconds + 1,
    minutes: Math.floor(seconds / 60)
  }))
}, 1000)

}

Renato
  • 1
  • 1
  • setInterval is not accurate for a timer – epascarello Oct 06 '20 at 14:49
  • Maybe you need to check if seconds reached 60, then reset to 1. ```seconds: seconds == 60 ? 1 : seconds + 1;```, and you need to keep adding Math.floor on the existing minutes value to be ```minutes: minutes + Math.floor(seconds / 60)``` – Elharony Oct 06 '20 at 14:53
  • Problem with the accepted answer of the dupe is it will drift..... – epascarello Oct 06 '20 at 15:00

0 Answers0