You can use the setTimeout() function in JavaScript to redirect to another page after a certain amount of time. The setTimeout() function takes two arguments: a function to execute and a time in milliseconds.

Here is an example that shows how to redirect to another page after 5 seconds:

setTimeout(function(){
   window.location.href = "https://www.deepdeveloper.in";
}, 5000);

The above code redirects the user to the website “http://www.deepdeveloper.in” after 5 seconds (5000 milliseconds).

Another way is to use setTimeout with an arrow function

setTimeout(() => {
    window.location.href = "http://www.example.com";
}, 5000);

You can also use setTimeout to redirect to a different page on your own website

setTimeout(() => {
    window.location.href = "/page2";
}, 5000);

It’s worth noting that the setTimeout function is non-blocking, meaning that it won’t stop the execution of the rest of the code, so other functions and events will continue to run while the timeout is counting down.

Also Read:

Categorized in: