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.
dolly answered on April 3rd 20 at 18:28
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 </>