Posted almost 4 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 whether this tab is visible:

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

Supported in real browsers and IE10. 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 to be notified if visibility changes.

Growing Rails Applications in Practice
Check out our new e-book:
Learn to structure large Ruby on Rails codebases with the tools you already know and love.

Owner of this card:

Henning Koch
Last edit:
almost 4 years ago
by Henning Koch
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 cookies to improve usability and analyze traffic.
Accept or learn more