2017-08-09 37 views
0

我已經在窗口加載時使用setInterval()實現了會話超時。如何重置按鍵事件的會話時間。這是我寫的代碼。如何在按鍵上重置會話

 

    
     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; 
     } 
     

+0

您是不是要找'keypress',而不是'kwepress'? –

回答

0
// 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 
} 
+0

當我們將調用whenIserIdle()? – Durga

+0

它會在'mousemove','keydown'或'click'事件發生時自動調用 –