<!DOCTYPE html>
<
html
>
<
head
>
<
title
>
How to Redirects to Another WebPage
if User Idle for 10 Seconds in
JavaScript ?
</
title
>
</
head
>
<
body
>
<
h
style
=
"color:green;"
>
Welcome To GFG
</
h
>
<
p
>Wait Idle For 10 Seconds To See Magic.</
p
>
<
script
>
(function () {
// Ideal time in seconds
const idleDurationSecs = 10;
// Redirect idle users to this URL
const redirectUrl =
// Variable to hold the timeout
let idleTimeout;
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
>
</
body
>
</
html
>