<iframe src="https://b.example.com" allow="screen-wake-lock"></iframe>Screen Wake Lock API / notebook
Screen Wake Lock API
Runnable notebook: every JavaScript, HTML and CSS example on this page executes in a sandboxed local frame. Nothing leaves the browser.
if ("wakeLock" in navigator) {
isSupported = true;
statusElem.textContent = "Screen Wake Lock API supported!";
} else {
wakeButton.disabled = true;
statusElem.textContent = "Wake lock is not supported by this browser.";
}// Create a reference for the Wake Lock.
let wakeLock = null;
// create an async function to request a wake lock
try {
wakeLock = await navigator.wakeLock.request("screen");
statusElem.textContent = "Wake Lock is active!";
} catch (err) {
// The Wake Lock request has failed - usually system related, such as battery.
statusElem.textContent = `${err.name}, ${err.message}`;
}wakeLock.release().then(() => {
wakeLock = null;
});wakeLock.addEventListener("release", () => {
// the wake lock has been released
statusElem.textContent = "Wake Lock has been released";
});document.addEventListener("visibilitychange", async () => {
if (wakeLock !== null && document.visibilityState === "visible") {
wakeLock = await navigator.wakeLock.request("screen");
}
});