[whatwg] window.setInterval if visible.

Markus Ernst derernst at gmx.ch
Thu Oct 15 13:53:54 PDT 2009


Gregg Tavares schrieb:
> I was wondering if there as been a proposal for either an optional 
> argument to setInterval that makes it only callback if the window is 
> visible OR maybe a window.setRenderInterval.
> 
> Here's the issue that seems like it needs to be solved.
> 
> Currently, AFAIK, the only way to do animation in HTML5 + JavaScript is 
> using setInterval. That's great but it has the problem that even when 
> the window is minimized or the page is not the front tab, JavaScript has 
> no way to know to stop animating.  So, for a CPU heavy animation using 
> canvas 2d or canvas 3d, even a hidden tab uses lots of CPU. Of course 
> the browser does not copy the bits from the canvas to the window but 
> JavaScript is still drawing hundreds of thousands of pixels to the 
> canvas's internal image buffer through canvas commands.

[...]
> 
> There are probably other possible solutions to this problem but it seems 
> like the easiest would be either
> 
> *) adding an option to window.setInterval or only callback if the window 
> is visible
> 
> *) adding window.setIntervalIfVisible (same as the previous option really)
> 
> A possibly better solution would be
> 
> *) element.setIntervalIfVisible
> 
> Which would only call the callback if that particular element is visible.

 From a performance point of view it might even be worth thinking about 
the contrary: Allow UAs to stop the execution of scripts on non-visible 
windows or elements by default, and provide a method to explicitly 
specify if the execution of a script must not be stopped.

If you provide methods to check the visibility of a window or element, 
you leave it up to the author to use them or not. I think performance 
issues should rather be up to the UA.



More information about the whatwg mailing list