How to Redirects to Another WebPage if User Idle for 10 Seconds in JavaScript ? Last Updated : 28 Dec, 2022 Summarize Comments Improve Suggest changes Share Like Article Like Report In this article, we will create a webpage that redirects to another page if the user is idle for more than one minute. We will use JavaScript to redirect the web page to another web page if the user is ideal for 10 seconds. Approach: To make this, we will use setTimeout() and clearTimeout() methods. The setTimeout() method is used to start the countdown for the ideal time and loads to another webpage when the countdown reaches 10 seconds. But if any of the events like clicking the mouse, moving the mouse, or an element is touched by the user (which is observed by the addEventListener function), the clearTimeout() method is called to stop the execution of the setTimeout after which the setTimeout() method restarts its countdown of idle activity from 0 seconds. At the end of 10 seconds, the webpage automatically redirects to the GFG home page. Syntax: const resetIdleTimeout = function () { if (idleTimeout) clearTimeout(idleTimeout); idleTimeout = setTimeout( () => location.href = redirectUrl, idleDurationSecs * 1000); }; resetIdleTimeout(); ['click', 'touchstart', 'mousemove'].forEach( evt => document.addEventListener(evt, resetIdleTimeout, false) ); Example: In this example, the web page redirects to another web page if the user idle for 10 Seconds in JavaScript HTML <!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 = 'https://www.geeksforgeeks.org/'; // 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> Output: Comment More infoAdvertise with us Next Article How to redirect to another webpage in HTML? P pritamauddy25 Follow Improve Article Tags : Technical Scripter JavaScript Web Technologies Technical Scripter 2022 JavaScript-Questions +1 More Similar Reads How to Redirect to Another Webpage using JavaScript? JavaScript can redirects the users from current page to another webpage (different URL) with and without requiring any manual action (like clicking a link). To redirect to another weboage in JavaScript we need to manipulate the window.location object and it will allow us to navigate programmatically 2 min read How to redirect to another webpage in HTML? It is often required to redirect to another webpage on loading a webpage, for example, due to the contents being moved to the new webpage. Demonstrated here are two simple methods to redirect to another webpage on load. Method-1: Using http-equiv attribute of the meta tag in HTML.Syntax:<meta htt 2 min read How to Redirect to Another Page After 5 Seconds using jQuery ? In this article, we will learn, how to redirect to another page or URL after a specified time in jQuery. The built-in function used for performing the action is as follows. The setTimeout(callback, delay) function takes two parameters a callback and a time in milliseconds. When this method is called 1 min read How to redirect to a relative URL in JavaScript? To redirect to a relative URL in JavaScript, you could use window.location.href. It is a property in JavaScript that represents the complete URL of the current page. It can be used to get the current URL or to navigate to a new URL by assigning a new URL to it. Approach HTML Structure:The HTML file 2 min read How to return true if browser tab page is focused in JavaScript ? There may be situations when we need to check if a browser tab is focused or not. The reasons for this include: Prevent sending network requests if the page is not being used by the user as this would reduce the amount of load on the server. Also, this would save upon the cost if we are using a paid 4 min read How can a page be forced to load another page in JavaScript ? In JavaScript, you can force a page to load another page by using the window.location object. There are a few methods to achieve this. To force a page to load another page in JavaScript, we have multiple approaches: Below are the approaches used to force a page to load another page in JavaScript: Ta 2 min read Like