I want my page to reload every so often, but if a user is inactive for say 5 minutes I want to stop the page from reloading. I have an example on http://codepen.io/tetonhiker/pen/gLeRmw .
Right now it's refreshing every 10 seconds. Again I want it to stop say after 5 minutes of refreshing when a user is inactive. How can I do this?
var timer = null;
$(function() {
// Get the time to stop the effect
var stopTime = new Date();
stopTime.setMinutes(stopTime.getMinutes() + 5);
// Get a reference to the timer so it can be cancelled later
timer = setInterval(function() {
// Check to see if the timer should stop
var currentTime = new Date();
if(currentTime < stopTime){
var randomnumber = Math.floor(Math.random() * 100);
$('#show').text(
'I am getting refreshed every minute..! Random Number ==> ' + randomnumber);
} else {
// Stop the timer
clearInterval(timer);
}
}, 60 * 1000);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="show" align="center"></div>
<div align="center">
</div>