Open In App

How to redirect to multiple websites with a delay using JavaScript ?

Last Updated : 12 Apr, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

We have given multiple websites and the task is to redirect to multiple websites with some delay using JavaScript. We will use setTimeout() function to delay the website.

setTimeout() Function: The setTimeout() method executes a function, after waiting a specified number of milliseconds.

The first parameter is a command/function to be executed and the second parameter indicates the delay time in milliseconds before execution.

For Example:

Javascript




console.log("Geeks");
setTimeout(() => {  console.log("forGeeks"); }, 3000);


This would log “Geeks” to the console, then after three seconds “forGeeks” and in many cases, we are required to some task then wait for some time and proceed another task, so in those cases, we can use the setTimeout method. 

Redirecting to multiple websites with some delay: Given the link to various websites, the task is to redirect to them with some delay time. Let the website URL’s be 

  • https://www.geeksforgeeks.org/
  • https://write.geeksforgeeks.org/
  • https://auth.geeksforgeeks.org/user/sahivam4u/profile
  • https://github.com/shivam7374

We have to redirect to all of them one after the other after a delay of 5 seconds. The above problem can be solved by using the below-given code:

HTML




<!DOCTYPE html>
<html>
  
<head>
    <script type="text/javascript">
        function Redirect() {
            const URLS = [
        'https://www.geeksforgeeks.org/',
            ];
  
            for (let i = 0; i < URLS.length; i++) {
                setTimeout(() => {
                    const a = document.createElement('a');
  
                    a.style.display = 'none';
                    a.href = URLS[i];
                    a.target = '_blank';
                    document.body.appendChild(a);
  
                    a.click();
                    a.remove();
                }, i * 5000);
            }
        }
    </script>
</head>
  
<body>
    <h2>Welcome To GFG</h2>
  
    <p>
        Click on the below button to 
        redirect to multiple websites
        after a delay time of 5 seconds.
    </p>
  
    <button onclick="Redirect();">
        Redirect
    </button>
</body>
  
</html>


Output:

Click on the Redirect Button to redirect to multiple websites.

When we run the above-given HTML code and click the Redirect Button the site automatically redirects to the above-defined Multiple Websites after a delay time of 5 seconds. This delay time and website to which the site redirects can be changed by changing the values in the script tag of HTML file. In the script tag, we are creating “a”(anchor) tag for every website link which is clicked due to which the website is redirected and the anchor tag is deleted after redirection and another tag is formed after a delay of 5 seconds till we redirect to all the multiple websites links given.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads