Open In App

How to Redirect to Another Page After 5 Seconds using jQuery ?

Last Updated : 14 Jul, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

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, the callback function will be executed after the specified time. With this method, the callback function is executed only one time after the specified time.

Our goal is to redirect to another page after 5 seconds and this will happen only one time. We use jQuery setTimeout() function

Example:

HTML




<!DOCTYPE html>
<html lang="en">
  
<head>
    <!-- using jquery library -->
    <script 
    </script>    
</head>
  
<body>
    <h1 style="color: green;">
      Geeksforgeeks
    </h1>
  
    <!-- Creating a button -->
    <div class="redirect">
        <button>Redirect me to GFG</button>
    </div>
  
    <!-- Script which will redirect 
         us to another page    -->
    <script>
        // click event on button
        $("button").click(function(){
            $(".redirect").text("Redirecting....")
  
            // storing url and time
            let delay = 5000;
            let url = "https://www.geeksforgeeks.org/";
            setTimeout(function(){
                location = url;
            },5000)
        })
    </script>
</body>
  
</html>


Output: After 5 seconds, the “geeksforgeeks” home page will open.



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads