tags:

views:

598

answers:

3

I want a jQuery countdown:

  1. It starts counting after page download finishes
  2. After counting to 0 it redirects to a url

How i can do that?

Thanks in advance

+7  A: 
$(function(){
  var count = 10;
  countdown = setInterval(function(){
    $("p.countdown").html(count + " seconds remaining!");
    if (count == 0) {
      window.location = 'http://google.com';
    }
    count--;
  }, 1000);
});
Jonathan Sampson
+1 for insanely fast response
czarchaic
@czarchaic: We specialize in those around here ;)
Jonathan Sampson
mmm, "setInerval -> setInterval". It would be included in such a case "clearInterval"?
andres descalzo
@andres, I just added a handler for the Interval for use in `clearInterval(countdown)`.
Jonathan Sampson
+3  A: 

Are you sure, you want to use Javascript for this? You might just go with plain HTML:

<meta http-equiv="refresh" content="NUMBER_OF_SECONDS_TO_WAIT; URL=http://REDIRECT_URL/"&gt;

Put this in the header and the forward will even work on browsers without Javascript enabled.

nfechner
if there is any image or problem loading the page and makes later to finish, "" NUMBER_OF_SECONDS_TO_WAIT "countdown continues or is expected to finish loading the page?
andres descalzo
+1  A: 

I have create a countdown script using jquery, css and html. Here is my full source code. Demo link also available.

CSS Part: body{ font-family: verdana; font-size:12px; } a{text-decoration: none;color:blue;font-weight: bold;} a:hover{color: gray;font-weight: bold;} div#my-timer{width: 400px;background: lightblue; margin: 0 auto;text-align: center;padding:5px 0px 5px 0px;}

Jquery Part:

<script type="text/javascript" src="js/jquery-1.4.2.js"></script>
    <script type="text/javascript">
        var settimmer = 0;
        $(function(){
                window.setInterval(function() {
                    var timeCounter = $("b[id=show-time]").html();
                    var updateTime = eval(timeCounter)- eval(1);
                    $("b[id=show-time]").html(updateTime);

                    if(updateTime == 0){
                        window.location = ("redirect.php");
                    }
                }, 1000);

        });
    </script>

Html Part:

<div id="my-timer">
        Page Will Redirect with in <b id="show-time">10</b> seconds        
</div>

Demo Link: http://demos.coolajax.net/php/redirect

I hope this code will be a helpful one for you.

Himel Khan