Umar Umar - 1 year ago 111
Javascript Question

How to automatically reload a page after a given period of inactivity

How can I automatically reload a webpage, if there have been no activity on the page for a given period of time?


If you want to refresh the page if there is no activity then you need to figure out how to define activity. Let's say we refresh the page every minute unless someone presses a key or moves the mouse. This uses jQuery for event binding:

     var time = new Date().getTime();
     $(document.body).bind("mousemove keypress", function(e) {
         time = new Date().getTime();

     function refresh() {
         if(new Date().getTime() - time >= 60000) 
             setTimeout(refresh, 10000);

     setTimeout(refresh, 10000);