jQuery Redirect to Another Page After 5 Seconds

In this section, we will learn how we can redirect to another page automatically after five seconds in jquery. After that, we will learn how to use jquery to redirect to url after 10, 15, 30 seconds, or any specified time. In order to redirect to url in jQuery after five seconds, we will use the JavaScript setInterval function. The setTimeout method and setInterval function are similar to each other. The setTimeout() is used to call the function only one time. The setInetrval is used to invoke them multiple times. Suppose we want to display a message every three seconds for the visitors of our website. When we use JavaScript setInterval() function on our website, we are able to incorporate new features on our websites.

The HTML button is contained by the HTML markup. In order to display the countdown timer, we will use HTML SPAN, which is contained by the HTML DIV. A click event handler of jQuery assigns to the button named "Redirected". After that, the JavaScript setInterval function will be initialized inside the click event handler so that it can execute every 1000 milliseconds that means every 1 second. In our example, every time there is an execution of the JavaScript setInterval function. Firstly, the value of the second variable will be decrement. After that, it will display the countdown timer HTML SPAN's value. The timer of the countdown will end when the second variable's value reaches 0, and then that page will be redirected to another page.

In our example, we will use 2 seconds to redirect to another page by creating a button. After clicking on the created button "Redirected", the countdown will be started, and that button will redirect to entered url page after 5 seconds. The following example shows us the redirection to another given url page in jQuery.

Example:

The output of the above example is as follows:

jQuery Redirect to Another Page After 5 Seconds

After click on this, we will be redirected to another page. Consider the following screenshot:

jQuery Redirect to Another Page After 5 Seconds