The request()
method of the WakeLock
interface returns a Promise
that resolves with a WakeLockSentinel
object, which allows control over screen dimming and locking.
A Promise
that resolves with a WakeLockSentinel
object.
The following asynchronous function requests a WakeLockSentinel
object. The request()
method is wrapped in a try...catch
statement to account for if the browser refuses the request for any reason.
const requestWakeLock = async () => {
try {
const wakeLock = await navigator.wakeLock.request("screen");
} catch (err) {
console.log(`${err.name}, ${err.message}`);
}
};
requestWakeLock();