NoSleep.js icon indicating copy to clipboard operation
NoSleep.js copied to clipboard

It doesn't work when re-focusing on tab

Open jester6san opened this issue 3 years ago • 4 comments

  • 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?

jester6san avatar May 17 '21 07:05 jester6san

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.

jovanchohan avatar May 17 '21 21:05 jovanchohan

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.

jovanchohan avatar May 19 '21 22:05 jovanchohan

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...

jester6san avatar May 24 '21 12:05 jester6san

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

vivipeder avatar Sep 06 '21 10:09 vivipeder