Many websites like recipe sites or tutorial sites have a legit desire to prevent the screen of a device from turning off. Since the user may be busy with performing a task (following the recipe or tutorial steps), in many cases they may not be able or willing to touch the screen to keep it awake, for example, because their hands are dirty with dough or glue. The current workaround for page authors to obtain the desired behavior is to play an invisible video and to loop it infinitely. The Wake Lock API instead provides them with a proper way to programmatically keep the screen awake via a screen wake lock. When the user navigates away from either the tab or multitasks away from their browser completely, the wake lock would be released automatically. Spec: https://w3c.github.io/wake-lock/ Article: https://web.dev/wakelock/ Demo: https://wake-lock-demo.glitch.me/ Sample code: ```js // The wake lock sentinel. let wakeLock = null; // Function that attempts to request a wake lock. const requestWakeLock = async () => { try { wakeLock = await navigator.wakeLock.request('screen'); wakeLock.addEventListener('release', () => { console.log('Wake Lock was released'); }); console.log('Wake Lock is active'); } catch (err) { console.error(`${err.name}, ${err.message}`); } }; // Request a wake lock… await requestWakeLock(); // …and release it again after 5s. window.setTimeout(() => { wakeLock.release(); wakeLock = null; }, 5000); ```
Our web app displays long running photo slideshows and we would love this feature. Like many other sites, we are currently forced to use the 'nosleep' library (https://github.com/richtr/NoSleep.js/) to keep the display awake, but a native solution would be preferable, and presumably much better for battery life. We also use the 'nosleep' library when users are performing long running file uploads (e.g. large selections of photos), because whenever the screen goes to sleep the upload dies. (I assume this problem would be better solved by allowing background uploads of some sort, but at the moment 'nosleep' seems to be the only option).
(The data upload feature would ideally be solved with Background Fetch: https://wicg.github.io/background-fetch/.)
<rdar://problem/71618179>
Our platform uses a queue where customers need to wait for to someone answer the call, like a call center. There we need to prevent the screen of a device from turning off or browser go to disconnect from the websocket. Use a hidden video with a loop is a hack, but is not respectful with battery.
Our site uses this for big downloads - The display must be kept on or the requests will fail