I am trying to make it so a div refreshes with ajax. The code itself is implemented and working already. I want the div to refresh every 30 seconds but only on an active tab. From what I understand setInterval will refresh every time regardless of whether the tab is in use or not. Id like to combine a mouseenter (or some other kind of user interaction that implies the user is active on the site) with setInterval so that the setInterval doesnt get fired if inactive.
currently I have this code which works well on the initial page load. There is no refresh during the first 30 seconds, nor is there a refresh until mouseenter on the div. However after the initial 30 seconds it refreshes on every mouseenter.
$(document).ready(function(){
$("#container").hide();
$("#loading").show();
$("div#refresh").load('example.com/load.php', function(){ $("#container").show(); $("#loading").hide(); });
function refresh() {
$("#refresh").mouseenter(function() {
// $("#container").hide();
$("#loading").show();
$("div#refresh").load('example.com/load.php', function(){ $("#container").show(); $("#loading").hide(); });
});
clearInterval(intervalID);
}
var intervalID = setInterval(refresh, 30000); // Will alert every 30 second.
// clearInterval(intervalID); // Will clear the timer.
});