[whatwg] window.setInterval if visible.

Gregg Tavares gman at google.com
Fri Oct 16 11:20:47 PDT 2009


On Thu, Oct 15, 2009 at 1:53 PM, Markus Ernst <derernst at gmx.ch> wrote:

> 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.
>

I agree that would be ideal. Unfortunately, current webpages already expect
setInternval to function even when they are not visible. web based chat and
mail clients come to mind as examples. So, unfortunately, it doesn't seem
like a problem a UA can solve on it's own.

On the otherhand, if the solution is as simple as add a flag to setInterval
then it's at least a very simple change for those apps that want to not hog
the CPU when not visible.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.whatwg.org/pipermail/whatwg-whatwg.org/attachments/20091016/8703a516/attachment-0002.htm>


More information about the whatwg mailing list