How to automatically reload a page after a given period of inactivity in JavaScript?

Estimated read time 1 min read

You can automatically reload a page after a given period of inactivity in JavaScript by using the setTimeout function:

// Set the time period of inactivity (in milliseconds)
var timeoutPeriod = 60000; // 1 minute

// Start a timer when the page loads
var timer = setTimeout(function() {
  // Reload the page after the timeout period has passed
  location.reload();
}, timeoutPeriod);

// Reset the timer when the user interacts with the page
document.addEventListener("mousemove", function() {
  clearTimeout(timer);
  timer = setTimeout(function() {
    location.reload();
  }, timeoutPeriod);
});

In this example, the timer is started when the page loads. Whenever the user interacts with the page (e.g. moving the mouse), the timer is reset. If the user does not interact with the page for the specified timeout period, the page will automatically reload. You can adjust the timeout period as needed to determine how long the page should wait before reloading.

You May Also Like

More From Author

+ There are no comments

Add yours

Leave a Reply