chrisguttandin / worker-timers

A replacement for setInterval() and setTimeout() which works in unfocused windows.
MIT License
590 stars 25 forks source link

[Feature Request] add `requestAnimationFrame` api #438

Open iShawnWang opened 3 months ago

iShawnWang commented 3 months ago

It is available https://developer.mozilla.org/en-US/docs/Web/API/DedicatedWorkerGlobalScope/requestAnimationFrame

chrisguttandin commented 3 months ago

Hi @iShawnWang, can you please elobarte a bit what you have in mind? What would be the benefit compared to using requestAnimationFrame() on the main thread directly?

ssjdan27 commented 3 months ago

If I had to guess, it seems like @iShawnWang wants the ability for requestAnimationFrame() to run in inactive tabs like what you have done here for the timers. If I recall correctly, requestAnimationFrame() does not run at all in inactive tabs and will completely stop until you go back to that tab. Could be of use, but I have not needed it myself.

chrisguttandin commented 2 months ago

Thanks for chiming in @ssjdan27.

My take is that requestAnimationFrame() is meant to run some code right before the browser renders something on the screen. When a website is not visible it will also not render anything on the screen and therefore it seems reasonable to me that requestAnimationFrame() never gets called.

If requestAnimationFrame() is used for something that doesn't render anything but should be called periodically it should probably be replaced by setTimeout() or setInterval().

But maybe there is a use case that I just haven't thought of yet. I'd be happy to be convinced otherwise.