title | slug | page-type | status | browser-compat | |
---|---|---|---|---|---|
WakeLock |
Web/API/WakeLock |
web-api-interface |
|
api.WakeLock |
{{APIRef("Screen Wake Lock API")}}{{SecureContext_Header}}{{SeeCompatTable}}
The WakeLock
interface of the Screen Wake Lock API prevents device screens from dimming or locking when an application needs to keep running.
The system wake lock is exposed through the global {{domxref('Navigator.wakeLock')}} property.
- {{domxref("WakeLock.request", "request()")}} {{Experimental_Inline}}
- : Requests a {{domxref("WakeLockSentinel")}} object, which returns a {{jsxref("Promise")}} that resolves with a {{domxref("WakeLockSentinel")}} object.
The following asynchronous function requests a {{domxref("WakeLockSentinel")}} object. The {{domxref("WakeLock.request", "WakeLock.request()")}} method is wrapped in a try...catch
statement to account for if the browser refuses the request for any reason.
try {
const wakeLock = await navigator.wakeLock.request('screen');
} catch (err) {
// the wake lock request fails - usually system related, such being low on battery
console.log(`${err.name}, ${err.message}`);
}
{{Specifications}}
{{Compat}}