Open In App
Related Articles

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

Like Article
Save Article
Report issue

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:


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 


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:


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



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.

Last Updated : 12 Apr, 2023
Like Article
Save Article
Share your thoughts in the comments
Similar Reads