0

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>
lostInTheTetons
  • 1,162
  • 1
  • 13
  • 23
  • It might be more clear if you change your wording to be 'updating the contents of the show' rather than saying reload or refresh, which is usually associated with a browser action. – Taplar Dec 06 '16 at 18:26
  • Possible duplicate of [Stop setInterval](http://stackoverflow.com/questions/16437173/stop-setinterval) – Heretic Monkey Dec 06 '16 at 18:29

4 Answers4

3

Just set a variable equal to the timer id and cancel when your condition is met:

var timer = null;

// As soon as the document is ready:
$(function() {
   // Activate the timer:
   startClock();
});

// And, when the user interacts with the page, start over
$(document).on("mousemove click", function(){ 
  // Stop the previously running timer
  clearInterval(timer);

  // Start the clock over again:
  startClock();
});

function startClock(){
  // 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 10 seconds..! " +
                      "Random Number ==> " + randomnumber);
    } else {
      // Stop the timer
      clearInterval(timer); 
    }
  }, 10000);
}
nominolo
  • 5,085
  • 2
  • 25
  • 31
Scott Marcus
  • 64,069
  • 6
  • 49
  • 71
2

You can achieve this in a couple different ways. First I am going to assume you already have logic existing to check if a person has been idle for 5 minutes (as that makes answering this easier, :D)

What you can do is when you call setInterval, store it's result in a variable. At which point if the user becomes idle for 5 minutes, window.clearInterval(thatVariable) to end the loop.

Otherwise, change your logic to use setTimeout instead and have it recursively call itself so long as the user has not been idle for 5 minutes.

Taplar
  • 24,788
  • 4
  • 22
  • 35
  • It looks like you are doing the clearInterval route. What exactly are you asking about? – Taplar Dec 06 '16 at 21:14
  • How could I get it to reset once a user is active again? – lostInTheTetons Dec 06 '16 at 21:16
  • Ah, well if you want to do that I would suggest putting that whole logic in a method. And then in the event that the user is active again you can call that method to start the whole process over again. – Taplar Dec 06 '16 at 21:17
  • any hints or tips on how to get that method started? Thanks for your help as well, much appreciated. – lostInTheTetons Dec 06 '16 at 21:18
  • You'd just call whatever method you stuck it in. If you put it in a method like function refreshShowText () { ... } then later when they go from inactive to active you'd just call refreshShowText() – Taplar Dec 06 '16 at 21:20
0
<script type="text/javascript">
    setTimeout(function () { 
      location.reload();
    }, 60 * 1000);
</script>

setTimeout will reload the page after a specified number of milliseconds, hence 60 * 1000 = 1m. Also, since the page is being refreshed, the timeout will always be set on page load.

Venkat
  • 316
  • 1
  • 13
0

An easy way is mix setInterval with setTimeout.

function refresh() {
    var randomnumber = Math.floor(Math.random() * 100);
    $('#show').text('I am getting refreshed every 10 seconds..! Random Number ==> ' + randomnumber);
}

var intervalId = setInterval(refresh, 10 * 1000);

setTimeout(function () {
    clearInterval(intervalId);
    alert('refresh stop!');
}, 5 * 60 * 1000);
rogeriolino
  • 1,095
  • 11
  • 21