Redirect user after 60 seconds of idling/inactivity?
Instead of using a plugin with unnecessary Kbytes, all you need is a simple function like this
(see explanation in comments):
<script>
(function() {
const idleDurationSecs = 60; // X number of seconds
const redirectUrl = '/logout'; // Redirect idle users to this URL
let idleTimeout; // variable to hold the timeout, do not modify
const resetIdleTimeout = function() {
// Clears the existing timeout
if(idleTimeout) clearTimeout(idleTimeout);
// Set a new idle timeout to load the redirectUrl after idleDurationSecs
idleTimeout = setTimeout(() => location.href = redirectUrl, idleDurationSecs * 1000);
};
// Init on page load
resetIdleTimeout();
// Reset the idle timeout on any of the events listed below
['click', 'touchstart', 'mousemove'].forEach(evt =>
document.addEventListener(evt, resetIdleTimeout, false)
);
})();
</script>
If you want to redirect to the home page (usually at /
), change '/logout'
to '/'
:
const redirectUrl = '/'; // Redirect idle users to the root directory
If you want to reload/refresh the current page, simply change '/logout'
in the code above to location.href
:
const redirectUrl = location.href; // Redirect idle users to the same page
I belive you are looking for something like this:
http://paulirish.com/2009/jquery-idletimer-plugin/
If you were to code that yourself, you would need to capture mouse and keyboard events and restart your timer after any of these events. If the timer ever reaches the threshold or counts down to 0 from the threshold you can reset the URL of the page.