1

In my current website I'm trying to make a cookie clicker type game... But when I run the collector for currency per second it slows down when I switch tabs...

Here is the script:

setInterval(ppsCollector,16.6666666667);
function ppsCollector() {
  var ppf = pps / 60;
  pounds += ppf;
  document.getElementById("poundCount").innerHTML = metricAbbreviate(pounds);
  document.getElementById("pps").innerHTML = metricAbbreviate(pps);
}

Is there any way to run this at the same rate when out of focus?

Thanks for all help in advance. :)

Froogo
  • 48
  • 6

1 Answers1

1

After doing some research on this issue it looks like browsers forcible increase the interval of background tabs in order to prevent the machine from slowing down if they have a bunch of tabs open. However there are some workarounds that you can implement (see here for complete list).

I recommend that you take a look at HTML5 Web Workers. The only con is that they cannot access the DOM directly so you are going to need to utilize the PostMessage method to send a message back to the parent JavaScript in order to update the desired elements.

Community
  • 1
  • 1
vbguyny
  • 1,170
  • 1
  • 10
  • 29