clear interval if criteria not met
<p>In a nut shell, I need to clear an interval for a function called <code>newsCheck</code> if the users not on the home page. Seems simple enough, i thought so too. This is causing me huge problems. I use a history api which is why the interval would not clear on page reloads and changes.</p>

<p>Here's what I have with the unimportant scripting left out </p>

<pre><code>//only run if on home page
if (url == 'Home' || url == ''  || url == 'home' || url == 'home.html' || url == 'Home.html'){

    //set up interval handler for cancellation when not on home 
    var handle=self.setInterval(function(){newsCheck()},1000);

    // ...

}//close if Home clause 
else
{
    handle = self.clearInterval(handle);
} 
</code></pre>

<p>Okay, <code>newsCheck</code> is a function that checks for new information from the database via ajax query (polling). This needs to be cancelled when not on home as theres no need for it.</p>

<p><strong>Extra info on history api</strong></p>

<p>I need to cancel the interval because I use a history API script to load in new pages therefore the function keeps on running even if we are not on the Home page.</p>

<p>To combat this I put this in the <code>loadContent</code> function of the history API.</p>

<pre><code>$.ajax({
         type: "GET",
         url: "AJAX/request_feed.js",
         dataType: "script"
        });
</code></pre>

<p>As you can see it runs whatever page we're on so that the <code>newsCheck</code> function can be cancelled. This is why I have the url check in the script above.</p>

<p>In the script with the <code>newsCheck</code> function I have put the url variable before the <code>(document).ready</code> so that the url is retrieved when the url has changed to the new url so it doesn't fetch the url of the previous page instead. Then we check for URL and run script if on <code>Home</code> and if not we clear the interval.</p>
