title: How to set AJAX interval so that it only refreshes if the page is still in focus?
body: <p>I created a little script to keep the PHP session active even if the user does not refresh the page.</p>
  
  <p>Here is the Javascript I'm using via PHP to keep the session alive:</p>
  
  <pre><code>echo 'setInterval(function(){$.post(\'/refreshTheSession.php\');},90000);';
  </code></pre>
  
  <p>It works fine, but I've noticed that it will keep calling the <code>refreshTheSession.php</code> script <em>even if the page is not in focus</em>, which I don't want because it means someone can leave a tab open with that page and keep the session alive indefinitely even if they are on a different tab doing something else.</p>
  
  <p>I only want the session to stay alive if the user is still actively on the page in question.</p>
  
  <p>Is it possible to do that?  If so, how can I modify my code above to do that?</p>