A page can stop being visible without its session being over. Someone may switch tabs to check a message, then return a moment later. Page visibility helps an application respond to that change, but it does not tell the application what the person intends to do next.
The browser exposes the document’s current state through document.visibilityState. The HTML Standard defines visible and hidden states and fires a visibilitychange event when the state changes. A background tab or a minimized browser window can lead to a hidden state. The event itself does not carry the new state, so read the property when handling it.
A small clock illustrates a practical use. Updating its display every second while the page is hidden is unnecessary. When the page becomes visible, render the current time immediately and restart the interval:
<p>Current time: <time id="clock"></time></p>
<script>
const clock = document.querySelector('#clock');
let timer = null;
function renderTime() {
clock.textContent = new Date().toLocaleTimeString();
}
function updateClockWork() {
if (document.visibilityState === 'visible') {
renderTime();
if (timer === null) timer = setInterval(renderTime, 1000);
} else if (timer !== null) {
clearInterval(timer);
timer = null;
}
}
document.addEventListener('visibilitychange', updateClockWork);
updateClockWork();
</script>
The initial call matters because an event listener only reacts to later changes. The timer guard avoids starting another interval if the update function runs while one is already active. On return, the clock reads the current time instead of trying to replay every missed tick. A data-driven display could similarly refresh from its source, while handling failures and stale responses.
Choose what to pause based on the task. Decorative animation or optional refreshes may be good candidates. An audio player, an upload, or a collaborative editing session may be expected to continue in the background. Visibility alone is not permission to interrupt work the person still wants.
A transition to hidden can also be a useful opportunity to initiate a small analytics send or save attempt. It is not a reliable declaration that the application has shut down, and neither an event handler nor a network request guarantees final delivery. A browser or operating system can stop a page before asynchronous work completes. Save important changes during normal use, handle errors, and design recovery around what was actually persisted.
Likewise, visible does not prove that the person is watching or interacting. Treat the state as a bounded browser signal: useful for conserving work and refreshing a display, but not evidence of attention or a guaranteed end-of-session notification.

The Campfire
CommentsThanks. Your comment is waiting for the desk. Once it is approved it appears here within about a minute.
Add a comment