I have a website which uses requestAnimationFrame
to constantly run a couple of functions. However, it won't run when the tab is hidden. I found a script online from https://stackoverflow.com/a/31105348/16855745, but this only worked for setTimeout. I used the following: requestAnimationFrame=function(t){setTimeout(t,1000/60)}
, which worked when paired with the above script, but I was wondering if there is any direct way to make requestAnimationFrame work when page is hidden.
Asked
Active
Viewed 758 times
1

brown john
- 11
- 2
-
1No there is not. requestAnimationFrame is designed exactly to not run in background pages. rAF is for things that are to be painted on the screen, when in background nothing is painted on the screen. – Kaiido Nov 10 '21 at 03:52
-
Is there a way that I can override `requestAnimationFrame` to use `setTimeout` only when the window isn't visible? – brown john Nov 11 '21 at 00:23
-
setTimeout is also throttled anyway, so that wouldn't help you much, but yes, you can use the visibilitychange event to determine if you need a hackish timers that doesn't get throttled, like a Web-Worker based one, or an AudioScheduledNode base one. – Kaiido Nov 11 '21 at 01:09