Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaScript setInterval() and minimized windows [duplicate]

It appears when you minimize a window or swap tabs, JavaScript's setInterval() method starts becoming incredibly unreliable.

I just finished working on a time tracking feature on an app I'm working on, which is driven by setInterval. This all worked perfectly until I started minimizing the window and coming back to it later, only to find out that the time had increased a small fraction of what it does when the window is up on the screen.

This seems to be a browser level "feature".

Is there an easy alternative I can swap this out for where units of time are actually honoured?

like image 979
JamesNZ Avatar asked Oct 19 '22 20:10

JamesNZ


1 Answers

That seems to be a feature in some browsers such a Chrome in that tabs which are inactive/in the background are allowed a timeout period no smaller than 1000 milliseconds. You can read about it here

The common solution is to use Web Workers, as they will not be affected by this issue. In fact a few such solutions have been shown here before.

like image 110
spac3nerd Avatar answered Oct 23 '22 09:10

spac3nerd