-2

Please read before marking this as a duplicate.

If I have setInterval running, the timer runs more or less consistently when the page is open, but if I open another application or another tab, the timer becomes about twice as slow. If I remember correctly, this has something to do with the computer prioritizing resources for the application/tab that's currently open.

Is there a way to let the timer run without slowing down once another tab or application is open?

The reason for a consistent timer is to broadcast data consistently. I have an app where an employer monitors employees through timers that the employees start. The data is broadcasted successfully, but the rate is slower once the employee opens another application.

Paprika
  • 91
  • 2
  • 9
  • Did you tried requestAnimationFrame() (https://developer.mozilla.org/en-US/docs/Web/API/window/requestAnimationFrame)? – grisuu May 24 '22 at 12:36
  • 1
    @grisuu - That's also throttled or even stopped when the tab doesn't have focus. – T.J. Crowder May 24 '22 at 12:36
  • setInterval should be called more often than your update. If you are updating something every second, try calling the interval every 1/4 or 1/2 a second and interpolate the current time with the last time the interval was called. – Mr. Polywhirl May 24 '22 at 12:36
  • 2
    What are you trying to do there that needs to be this consistent…? – deceze May 24 '22 at 12:37
  • 1
    @Paprika Ok. Next Idea: you can make use of a WebWorker. Thats "like" a Background Thread which does not run on the UI Thread – grisuu May 24 '22 at 12:43
  • 1
    If it's for monitoring employees, I don't understand why a slower rate of broadcasting the data would be a problem. Surely there won't be any user interaction in the browser that needs broadcasting while it is running in the background? – DBS May 24 '22 at 12:45
  • @deceze I've updated the description. But grisuu answered with the solution. – Paprika May 24 '22 at 12:46
  • @grisuu Thank you! This seems to be the right solution for the problem. You're help is appreciated :) – Paprika May 24 '22 at 12:47
  • @DBS The timers that monitor what the employee is doing will run in the background. If an employee has to open another application to do their work, the data still needs to be updated every second on the employer's dashboard. – Paprika May 24 '22 at 12:52
  • *Every second?!* That sounds… crazy… You'll get network hiccups which will suppress updates for a few seconds or minutes every now and then, so nothing can guarantee an update every single second anyway. – deceze May 24 '22 at 12:54
  • @deceze I figured that might be an issue with standard HTTP requests. I'm using WebSockets to deal with this issue, but if it does cause hiccups that are too noticeable, I'll definitely have to reduce the speed at which the data is broadcast. But if you believe this will still be an issue with WebSockets, I'll reduce the update rate right away. – Paprika May 24 '22 at 12:59
  • 1
    It's only a technical issue to some degree. The use case for updates every second needs to be clear. What's the point? What would happen if you used lower frequency updates? Would that be terrible? Is it absolutely paramount that you send updates every second? If so, what *does* happen in case of network hiccups? You need to deal with that scenario anyway. — If you can determine that a slower update frequency will do just fine as well, then… just go with the simplest possible solution of a `setInterval`, even if that gets throttled. It saves resources and… is simple. – deceze May 24 '22 at 13:02

0 Answers0