Posted almost 6 years ago. Visible to the public. Repeats.

Stop animations and network polling when the document tab isn't visible

Is your application doing something expensive every few seconds? Maybe an animated slider that rotates images? Maybe you are updating data over the network every five minutes?

It's a good idea to pause this if the your document tab is not even visible to the user. This saves your user's battery and data plan.

You can ask document.visibilityState Archive whether this tab is visible:

Copy
function pulse() { if (!document.visibilityState || document.visibilityState == 'visible') { // do expensive thing } } setInterval(5000, pulse);

Supported in real browsers and IE10 Archive . The !document.visibilityState in the code above assumes that the page is visible in case you have an old IE.

You can also subscribe to the visibilitychange event Archive to be notified if visibility changes.

Does your version of Ruby on Rails still receive security updates?
Rails LTS provides security patches for unsupported versions of Ruby on Rails (2.3, 3.2, 4.2 and 5.2).

Owner of this card:

Avatar
Henning Koch
Last edit:
over 5 years ago
by Henning Koch
Keywords:
tab, window, background
About this deck:
We are makandra and do test-driven, agile Ruby on Rails software development.
License for source code
Posted by Henning Koch to makandra dev
This website uses short-lived cookies to improve usability.
Accept or learn more