JavaScript

How to keep the screen awake using JavaScript

Advertisements

Some new features in JavaScript are great to see, and the wake lock API is one of those. It allows us to interact with the host system and can help the developer instruct the operating system to keep the screen awake using JavaScript!

This can be particularly useful for cases where you want the user’s device to not get locked due to inactivity timeouts. It can be useful for the browser to instruct the operating system that the user is engaging in an activity, such as recording something or watching a video.

The Wake lock API can then be used to prevent the screen from sleeping or being dimmed. For requesting the permission:

let screenLock;

try {
  screenLock = await navigator.wakeLock.request('screen');
} catch (err) {
  console.log('Error with wake lock: ', err);
}
JavaScript

If the request succeeds, the host machine will not sleep until the wake lock gets programmatically released. And that is pretty much all that is needed to keep the screen awake using JavaScript.

We wrap the code in a try-catch because the Screen Wake Lock request can be rejected if the host device is in power saver mode, or has a low battery, or the current tab is not visible.

For releasing the wake lock:

await screenLock.release()
JavaScript

It is also worth noting that the lock gets released automatically if the document becomes inactive (user changes tabs/windows etc.). To handle this, we could add a visibilitychange event:

document.addEventListener('visibilitychange', async () => {
  try {
    screenLock = await navigator.wakeLock.request('screen');
  } catch (err) {
    console.log('Error with wake lock: ', err);
  }
});
JavaScript

Note:

A couple of things to keep in mind:

  • The API is only available when the application is served over HTTPS.
  • Though this API has been around for quite some time, not all browsers support it. You can check MDN compatibility to see which ones do. At the time of writing this article, firefox and Safari on iOS are not supported.

This is a great feature to use on devices to ensure that the device does not go to sleep.

Saransh Kataria

Born in Delhi, India, Saransh Kataria is the brain behind Wisdom Geek. Currently, Saransh is a software developer at a reputed firm in Austin, and he likes playing with new technologies to explore different possibilities. He holds an engineering degree in Computer Science. He also shares his passion for sharing knowledge as the community lead at Facebook Developer Circle Delhi, NCR which is a developer community in Delhi, India.

Share
Published by
Saransh Kataria

Recent Posts

How To Get The Hash of A File In Node.js

While working on a project, I wanted to do an integrity check of a file…

2 weeks ago

Native popover API in HTML

Popovers have been a problem that was typically solved by using a third-party solution. But…

3 weeks ago

Node.js 20.6 adds built-in support for .env files

Node.js 20.6 added built-in support for the .env file. This is an excellent addition to the platform…

4 weeks ago

Object destructuring in TypeScript

Object destructuring is a powerful ES 6 feature that can help developers write cleaner code.…

1 month ago

Improve git clone performance in a CI pipeline

Have you felt particularly annoyed by the time it takes to clone a large repository,…

2 months ago

Fix: Hydration failed because the initial UI does not match what was rendered on the server

Within a React or Next.js app, if you encounter the error "Hydration failed because the…

2 months ago
Advertisements