javascript - How can I make setInterval also work when a tab is inactive in Chrome? -


i have setinterval running piece of code 30 times second. works great, when select tab (so tab code becomes inactive), setinterval set idle state reason.

i made simplified test case (http://jsfiddle.net/7f6dx/3/):

var $div = $('div'); var = 0;  setinterval(function() {     a++;     $div.css("left", a) }, 1000 / 30); 

if run code , switch tab, wait few seconds , go back, animation continues @ point when switched other tab. animation isn't running 30 times second in case tab inactive. can confirmed counting amount of times setinterval function called each second - not 30 1 or 2 if tab inactive.

i guess done design improve performance, there way disable behaviour? disadvantage in scenario.

on browsers inactive tabs have low priority execution , can affect javascript timers.

if values of transition calculated using real time elapsed between frames instead fixed increments on each interval, not workaround issue can achieve smother animation using requestanimationframe can 60fps if processor isn't busy.

here's vanilla javascript example of animated property transition using requestanimationframe:

var target = document.queryselector('div#target')  var startedat, duration = 3000  var domain = [-100, window.innerwidth]  var range = domain[1] - domain[0]    function start() {    startedat = date.now()    updatetarget(0)    requestanimationframe(update)  }    function update() {    let elapsedtime = date.now() - startedat      // playback value between 0 , 1    // being 0 start of animation , 1 end    let playback = elapsedtime / duration      updatetarget(playback)        if (playback > 0 && playback < 1) {    	// queue next frame    	requestanimationframe(update)    } else {    	// wait while , restart animation    	settimeout(start, duration/10)    }  }    function updatetarget(playback) {    // uncomment line below reverse animation    // playback = 1 - playback      // update target properties based on playback position    let position = domain[0] + (playback * range)    target.style.left = position + 'px'    target.style.top = position + 'px'    target.style.transform = 'scale(' + playback * 3 + ')'  }    start()
body {    overflow: hidden;  }    div {      position: absolute;      white-space: nowrap;  }
<div id="target">...here go</div>


for background tasks (non-ui related)

@upthecreek comment:

fine presentation issues, still there things need keep running.

if have background tasks needs precisely executed @ given intervals, can use html5 web workers. take @ möhre's answer below more details...

css vs js "animations"

this problem , many others avoided using css transitions/animations instead of javascript based animations adds considerable overhead. i'd recommend jquery plugin let's take benefit css transitions animate() methods.


Comments

Popular posts from this blog

java - Run a .jar on Heroku -

java - Jtable duplicate Rows -

validation - How to pass paramaters like unix into windows batch file -