I want to enable wake lock API. It is working fine with Windows platform and android devices but not in Safari or iOS devices.
Console has error NotAllowed, Permission was denied
in Safari Browser.
I tried with below code
if ('wakeLock' in navigator && 'request' in navigator.wakeLock) {
let wakeLock = null;
const requestWakeLock = async () => {
try {
wakeLock = await navigator.wakeLock.request('screen');
wakeLock.addEventListener('release', (e) => {
console.log(e);
console.log('Wake Lock was released');
});
console.log('Wake Lock is active');
} catch (e) {
console.error(`${e.name}, ${e.message}`);
}
};
requestWakeLock();
const handleVisibilityChange = () => {
if (wakeLock !== null && document.visibilityState === 'visible') {
requestWakeLock();
}
};
document.addEventListener('visibilitychange', handleVisibilityChange);
}
New contributor
Shriyank Mendpara is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.