prevent screensaver / sleep mode on iPhone and iPad

The problem: I created a website which requires the user to watch an animation for five minutes without doing anything else, ie touch the screen. This works just fine on a desktop computer, but mobile is a different story. When sleep mode is set to be activated in less than 5 minutes, it interrupts the script and the user has to start anew. I was convinced that there was no way to stop the screensaver from appearing but, as it turns out, there actually is: there is a neat javascript trick to prevent the screensaver from appearing!
The solution: attempt a redirect but stop it from actually happening:

iosSleepPreventInterval = setInterval(function () {
  window.location.href = the_path;
  window.setTimeout(function () {
    window.stop()
  }, 0);
}, 30000);

Thanks a lot for Stackoverflow!

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.