How to Redirect to Another Page After 5 Seconds using jQuery ?
Last Updated :
14 Jul, 2021
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 >
< script
</ script >
</ head >
< body >
< h1 style = "color: green;" >
Geeksforgeeks
</ h1 >
< div class = "redirect" >
< button >Redirect me to GFG</ button >
</ div >
< script >
// click event on button
$("button").click(function(){
$(".redirect").text("Redirecting....")
// storing url and time
let delay = 5000;
setTimeout(function(){
location = url;
},5000)
})
</ script >
</ body >
</ html >
|
Output: After 5 seconds, the “geeksforgeeks” home page will open.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...