Durga
Durga

Reputation: 565

How to reset session on keypress

I have implemented session timeout using setInterval() when the window loaded. How to reset session time on keypress event. Here is the code that I've written.


    
         window.onload = function(){
         (function(){
           var counter = 60;
           setInterval(function() {
             counter--;
             if (counter >= 0) {
                //alert(counter)
                span = document.getElementById("count");
                span.innerHTML = counter;
             }    

             if (counter === 0) { 
                 $("#session-expired-modal").modal('show');
             } 
           }, 1000);
          })();
        }
        function sessionExpiredRedirect(){
          window.location=url;
        }  
     

Upvotes: 0

Views: 1312

Answers (1)

user6448640
user6448640

Reputation:

// Using jQuery (but could use pure JS with cross-browser event handlers):
var idleSeconds = 30;

$(function(){
  var idleTimer;
  function resetTimer(){
    clearTimeout(idleTimer);
    idleTimer = setTimeout(whenUserIdle,idleSeconds*1000);
  }
  $(document.body).bind('mousemove keydown click',resetTimer); //space separated events list that we want to monitor

  resetTimer(); // Start the timer when the page loads
});

function whenUserIdle(){
  //...your code
}

Upvotes: 0

Related Questions