JS Timeout Event

Hi Experts,

If anyone can tell me how I can code a JS (or jQuery) event for timeout, let say for 10 minutes if there is no activity to do a self.location = 'timeout.html'

However, when the user click something or types, the timer to go back to 10 minutes.

Thank you
APD TorontoAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

zephyr_hex (Megan)DeveloperCommented:
Here is a simple example using setTimeout() and clearTimeout():

$(document).ready(function() {
  var timer;

  function setTimer() {
    timer = setTimeout(function() {
      alert("Time Is Up!");
    }, 5000);
  }

  $('form :input').on('keyup', function() {
    clearTimeout(timer);
    setTimer();
  });
  
  setTimer();
});

Open in new window


So, every time a keyup event occurs, the timer gets reset.  If you have other types of elements on your form (drop down, checkbox, etc), you will also need to look for the related events (such as the change event).
0
Julian HansenCommented:
Megan has presented a good example - here is another approach for reference. Rather than reseting the timer use setInterval and check a flag on each timeout.
<script>
$(function() {
  var TIMEOUT = 10,    // SET TO NUMBER OF INTERVALS TO CHECK
      INTERVAL = 60000, // 1 MINUTE INTERVALS
      tickCount = 0,   
      timeoutURL = 't2778.timeout.html', // URL TO REDIRECT TO ON TIMEOUT
      timer = setInterval(checkTimer, INTERVAL);

  $(this).mousemove(function() {
    tickCount = 0;
  });
  $(this).keypress(function() {
    tickCount = 0;
  });

  function checkTimer() {
    if (++tickCount >= TIMEOUT) {
      clearInterval(timer);
      window.location = timeoutURL;
    }
  }
});
</script>

Open in new window

You can see it working here
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
APD TorontoAuthor Commented:
Julian, with $(this) will that consider all activities and not this form element?
0
Julian HansenCommented:
Yes - at the global level $(this) is analogous to $(document).
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.