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. <br><br>Here's the issue that seems like it needs to be solved.<br>
<br>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.<br>
<br>To see an example run this sample in any browser<br><br><a href="http://mrdoob.com/projects/chromeexperiments/depth_of_field/">http://mrdoob.com/projects/chromeexperiments/depth_of_field/</a><br><br>Minimize the window or switch to another tab and notice that it's still taking up a bunch of CPU time.<br>
<br>Conversely, look at this flash page.<br><br><a href="http://www.alissadean.com/">http://www.alissadean.com/</a><br><br>While it might look simple there is actually a lot of CPU based pixel work required to composite the buttons with alpha over the scrolling clouds with alpha over the background.<br>
<br>Minimize that window or switch to another tab and unlike HTML5 + JavaScript, flash has no problem knowning that it no longer needs to render.<br><br>There are probably other possible solutions to this problem but it seems like the easiest would be either<br>
<br>*) adding an option to window.setInterval or only callback if the window is visible<br><br>*) adding window.setIntervalIfVisible (same as the previous option really)<br><br>A possibly better solution would be<br><br>*) element.setIntervalIfVisible<br>
<br>Which would only call the callback if that particular element is visible.<br><br>It seems like this will be come an issue as more and more HMTL5 pages start using canvas to do stuff they would have been doing in flash like ads or games. Without a solution those ads and games will continue to eat CPU even when not visible which will make the user experience very poor.<br>
<br>Am I making an sense?<br><br>-gregg<br><br><br><br><br>