Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Below is the working example based on above:

Code Block
let wakelock;
const canWakeLock = () => 'wakeLock' in navigator;

async function lockWakeState() {
  if(!canWakeLock()) return;
  try {
    wakelock = await navigator.wakeLock.request();
    wakelock.addEventListener('release', () => {
      console.log('Screen Wake State Locked:', !wakelock.released);
    });
    console.log('Screen Wake State Locked:', !wakelock.released);
  } catch(e) {
    console.error('Failed to lock wake state with reason:', e.message);
  }
}

function releaseWakeState() {
  if(wakelock) wakelock.release();
  wakelock = null;
}

await lockWakeState();
setTimeout(releaseWakeState, 5000); // release wake state after 5 seconds

...