title: prevent safari from sleep using javascript
body: <p>I am implementing a javascript timer the timer loads in an iframe and that timer also plays some audio files at certain intervals, basically it is for routine exercises so say for 10 seconds there is first exercise and then for a minute the second one and for 2 minutes the third one and so on.</p>
  
  <p>As the time for a exercise completes and the next one turns up I change the audio for that particular exercise and so on. What I want is to prevent the browser to sleep in each and every mode that is phone(android, iOS), laptop everywhere till the time the timer is running.</p>
  
  <p>I have used 2 solutions for ios I am using to reload the page and for the others whether it is android or laptop I am trying a video as stated <a href="https://github.com/ivanmaeder/computer-sleep" rel="nofollow noreferrer">here</a>.</p>
  
  <p>And my final code to check things is </p>
  
  <pre><code>var ua = {
    Android: /Android/ig.test(navigator.userAgent),
    // iOS: /AppleWebKit/.test(navigator.userAgent) &amp;&amp; /Mobile\/\w+/.test(navigator.userAgent)
    iOS:/iPhone|iPad|iPod/i.test(navigator.userAgent)
  };
  
  &lt;script type="text/javascript" src="js/sleep.js"&gt;&lt;/script&gt;
  
  if (ua.iOS) {
    preventIosSleep = setInterval(function(){
      window.top.location.href = '/';
      setTimeout(function(){
        try {
          window.top.stop();
        } catch (exception) {
          document.execCommand('Stop');
        }
      }, 0);
    }, 10000);
  } else {
    sleep.prevent();
  }
  
  // and when releasing the things
  if (ua.iOS) {
    clearInterval(preventIosSleep);
  } else {
    sleep.allow();
  }
  </code></pre>
  
  <p>The sleep.js is taken from the link as told above. Have been trying things for around a week now. This solution works well on all browsers except the safari on MAC. Can any one tell me how can I stop the safari browser not to enter the sleep mode.</p>