Visibility.js is a small JavaScript library that wraps the native Page Visibility API to give developers an easy interface for detecting when a user’s browser tab becomes visible, hidden, or is in prerendering mode. Its main purpose is to let web applications adapt behavior depending on whether the page is actually being viewed — for example, pausing animations, postponing heavy computations, or stopping unnecessary network requests when the user switches to another tab. The library hides browser vendor-prefixes and normalizes visibility events so you don’t have to manage cross-browser quirks yourself. On top of simple visibility detection, Visibility.js adds useful helpers such as a timer function that only runs when the page is visible. As a result, developers can write resource-conscious web apps that reduce CPU/bandwidth usage when the user isn’t actively viewing the page.
Features
- Simple abstraction over the Page Visibility API with normalized events and states
- Ability to run timers (every) only when the page is visible, saving resources when hidden
- Callbacks like onVisible, onHidden, change to execute logic on visibility state changes
- Fallback mode using focus/blur events for browsers lacking native Page Visibility support
- Easy installation via npm and straightforward API usage for web developers
- MIT-licensed and sufficiently lightweight for inclusion in most web apps