0

I have a simple function that converts seconds into the format HH:MM:SS, I am using setInterval() to run each second. Surprisingly, when executing the page for the first time, the clock seems to be working fine. However, when leaving it for a while and opening another window/tab in the browser and going back to it, the clock gets crazy and the setInterval seems to be executing 3 or 4 times each second.

formatTimeToHours(seconds){
return new Date(seconds * 1000).toISOString().substr(11, 8);
}

 clock(){
 let number = 0;
setInterval(()=>{
     number += 1;
     this.timer = this.formatTimeToHours(number)
}, 1000)
}
Folky.H
  • 1,164
  • 4
  • 15
  • 37

0 Answers0