As Loktar suggested, this symptom could be alleviated by checking the time and figuring out where you should be. Here is a function that could do this:
function smartInterval(func, interval){
var last = new Date() - interval,
now,
numMissed;
(function iterate(){
func();
// compute the number of iterations you might have missed
// if you tabbed away and the interval was restricted to 1000ms
now = +new Date();
numMissed = Math.round((now - last) / interval) - 1;
// make up for those iterations that were lost
while (numMissed--) { func(); }
last = +new Date();
setTimeout(iterate, interval);
})();
}
Usage:
smartInterval(function(){console.log('hi');}, 100);
Here is a jsFiddle with an example. Try to comment out the while
loop (while (numMissed--)
) to see that by switching to a tab, you get less numbers. With the while
loop there, however, it appears as if the interval has never changed.
Unfortunately, this might not be of any use to you since you are using Processing.js and the timeouts are set internally.
setTimeout
to my knowledge. However if im wrong and they are usingrequestAnimationFrame
instead it would make sense, since that stops completely when the tab is inactive. – Dowd