NoSleep.js
NoSleep.js copied to clipboard
It doesn't work when re-focusing on tab
- open an url that is running NoSleep.js -> everything works as expected
- "minimize" the tab, or switch to another app etc.
- switch back to the browser/tab running NoSleep.js -> it doesn't work anymore, phone goes to sleep
Tested on iOS 14.5.1, Google Chrome and Safari. I've tried enabling and disabling, initialising it again using window.onblur and window.onfocus events. Are there any solutions for this?
I've noticed the same thing. On iOS Safari the device falls asleep after being backgrounded and foregrounded even though when I drag down from the top of the iPhone to see that menu I can see the video playing on a loop. I've also noticed that when the device is charging this isn't an issue, but when the device is not charging then backgrounding and then foregrounding does not work.
What I've found is that after a tab becomes hidden and then visible again (as can be determined by the PageVisibility API), you need to capture a new user interaction to play the hidden background video to prevent the device from falling asleep.
Hey I tried that, but it fails to resume or re-initialize the plugin. I've only tested this on iPhone since that's the main platform that's going to run my project...
Try this code:
var btnNoSleep = document.createElement('button');
btnNoSleep.onclick = function() { if (noSleep.isEnabled()) { noSleep.disable(); }
noSleep.enable();
};
document.addEventListener('visibilitychange', function() { if (document.visibilityState == "visible") { btnNoSleep.click(); } });
Apparently it works for me on Safari IOS 14.7.1