richtr / NoSleep.js

Prevent display sleep and enable wake lock in any Android or iOS web browser.
MIT License
2.23k stars 380 forks source link

After using power button, NoSleep stops working. #81

Open skibulk opened 4 years ago

skibulk commented 4 years ago

Hello. Using your demo page, if I hit the power button on my phone to turn it off, then turn it back on, screen lock is re-enabled. I expected that since a user event is required to enable NoSleep, however, if I then manually disable and re-enable NoSleep, the screen still turns off after a minute or so. I'm using Safari on an iPhone 6, IOS 12.4.3.

I'm currently solving this by re-instantiating NoSleep and requiring the user to click in the app to re-enable it. Is the first instance of NoSleep / the first video kept in memory though?

window.addEventListener("blur", function(event){
    if(appRunning) {
        stopApp(); // disables noSleep
        noSleep = new NoSleep();
    }
});

Proposed solution: On blur, within the NoSleep instance itself, automatically disable NoSleep, remove the old video element, and create a new video element.

followdarko commented 4 years ago

I solve the problem like this

document.addEventListener('visibilitychange', function () { handleVisibilityChange(noSleep) })

function handleVisibilityChange (noSleep) {
  if (document.hidden) {
    noSleep.disable()
  } else {
    document.addEventListener('click', function enableNoSleep () {
      document.removeEventListener('click', enableNoSleep)
      noSleep.enable()
    })
  }
}
Perryyang1979 commented 2 years ago

I have same problem on ipad 14.6 but is ok on version 14.4, I used window.addEventListener("blur", function(event){ noSleep = new NoSleep(); noSleep.enable();
});

to create new instance and enable it , it still does not work on ipad Pro v14.6

who have any suggstion .