Jquery Countdown Script

So, I had the need the other day for one of those “if your page does not redirect in X seconds, click here” scripts where the X would count down.

here is a little piece of jquery i wrote to handle this.

Drop this is a script block

function countDown() {
var secs = $(”#secs”).html();
var newsecs = secs – 1;
if (newsecs >= 0) {
$(”#secs”).html(newsecs);
setTimeout(”countDown()”, 1000);
}
}
setTimeout(”countDown()”, 1000);

then in the body of your html drop in

<p>If the page does not redirect in <span id=”secs”>10</span> seconds, you may <a href=”/default.aspx”>click here</a> to continue.</p>

the script will look at the value inside the “secs” span and do a simple countdown starting with that value. Now, if you wanted the script to redirect after the countdown was complete simply change this line in the script file

if (newsecs >= 0) {
$(”#secs”).html(newsecs);

to this.

if (newsecs >= 0) {
$(”#secs”).html(newsecs);
else
window.location = “http://google.com”;

That’s all there is to it. pretty simple.

Comments are closed.