Do you know you can earn heaps of $$ by answering questions on kodlogs, for more info contact us through feedback link at footer? We are Australian based web site and pay you for answering questions

  • Register
Welcome to Kodlogs Q&A, where you can ask programming questions and receive answers from other members of the community. For programmers, by programmers Kodlogs is an open community for anyone that does coding. We help you get answers to your toughest coding questions, share knowledge with your coworkers in private, and find your next IT dream job.
0 votes
4 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)  
...