How to make a stopwatch is not stopped when switching to another from home?

Hi all, there is such code:
5df8ebdc609d6000679206.jpeg
How to make the transition to another browser tab and later return the stopwatch to work correctly.
April 3rd 20 at 18:24
2 answers
April 3rd 20 at 18:26
Solution
The logic is the following.
When you create the timer, get time to start it and put it in localStorage, when determining the inactive state of the page write this time there. And calculate the time difference continues to be considered with the right time.
April 3rd 20 at 18:28
Solution
It's a bad stopwatch, wrong.
setInterval(foo, 1000) will trigger every second exactly, and at LEAST a second, and will accumulate error.
On good it is necessary to store the start (or finish) time, on every tick compare the current time (new Date()) from the start, and then to draw the numbers on the display.

Then the problem with switching to another tab is solved very simply - the starting time is written in localStorage, for example. When you change the page it just read out, and the timer continues running in normal mode.

Do not post code and screenshots, use the button </>

Find more questions by tags JavaScriptReact