Lesson 9 of 9
Page Visibility & Screen APIs
The Page Visibility API detects when a user switches away from your tab - allowing you to pause videos, stop animations, save battery, and track actual time spent on page. The Screen Wake Lock API prevents the screen from sleeping during presentations, video playback, or recipe cooking steps.
Visibility & Screen APIs
- document.hidden - Boolean. true when tab is hidden (user switched away).
- visibilitychange event - Fires when tab visibility changes. Use to pause/resume media.
- Pause video when tab hidden - Save bandwidth and battery. Resume when user returns.
- Stop animations when hidden - CSS animations continue even in hidden tabs. Use visibility API to pause JavaScript animations.
- Track actual time on page - Start a timer when visible, pause when hidden. More accurate than page load duration.
- Screen Wake Lock - navigator.wakeLock.request('screen') keeps screen awake. Great for recipe apps, presentations, music players.
- Release wake lock - await sentinel.release() when no longer needed (e.g., video ends).
Diagram
Loading diagramโฆ
Pause animations and network polling when browser tabs are hidden to save power
Module 8 - Knowledge Check
Module 8 - Knowledge Check
Question 1 of 5What is the key difference between localStorage and sessionStorage?
Key Takeaways
- The Page Visibility API detects when a user switches away from your tab - allowing you to pause videos, stop animations, save battery, and track actual time spent on page.
- document.hidden - Boolean. true when tab is hidden (user switched away).
- visibilitychange event - Fires when tab visibility changes. Use to pause/resume media.
- Pause video when tab hidden - Save bandwidth and battery. Resume when user returns.
Finished this lesson?
0 of 9 in Module 8