• Register
Welcome to Kodlogs, programming questions and answer website.
0 votes
8 views
I want to redirect page after some time in javascript, lets say i want to redirect after specific time ie 5 seconds or redirect after 2 seconds, how do i do this? If someone can show me to do redirect after 5 seconds with coundown that would be good too?
by (930 points)  

2 Answers

0 votes

Some times there is need to redirect users to another page after specific time ie 3 secs or 5 seconds, there are number of methods to achieve this.

In following code we have button to redirect after 5 seconds, you can change timings as per your requirement

Javascript Code for redirects after 5 Seconds

HTML

<div id="redirect5SecMsg"></div>

<input type="button" onclick="redirect5Sec()" value="Click to Redirect"/>

<script>

function redirect5Sec(){

    document.getElementById('redirect5SecMsg').innerHTML = 'Redirecting after <span id="countNumbers">5</span> seconds....';

    var count = 5;

    setInterval(function(){

        i--;

        document.getElementById('countNumbers').innerHTML = i;

        if (i== 0) {

            window.location = 'https://www.kodlogs.com'; 

        }

    },1000);

}

</script>

by (930 points)  
0 votes
There is another way to redirect user to another pager after certain delay:

Use setTimeout to achieve this kind of delay

$(function redirectafterdelay() {

  setTimeout(function() {

    window.location.replace("../main.html");

  }, 5000);

});

Or you can use

setTimeout(function(){ window.location = "http://www.kodlogs.com"; },5000);
by (930 points)  
...