Shaun Mccran

My digital playground

09
F
E
B
2012

JQuery Countdown timer Plugin example

I'd previously used a JavaScript script to count down from a now() to a given date to display real time count down information on screen. I was cleaning up the project it was used on and wondered how easy it would be to push all the 'normal' JavaScript into a JQuery plugin and encapsulate it all into one function call.

Let me start off by saying this is the first script I've bothered to convert from traditional JavaScript into a more friendly JQuery format. It may not be anywhere near best practice for plugins, so feel free to point out the shortcomings. (Also I never checked if there is an existing plugin that does anything similar).

Both versions work in exactly the same way, and I probably cut about a third of the code base so I'm quite happy with the result.

view plain print about
1<s/cript src="jquery.countdown.js" language="javascript"></script>
2
3$(document).ready(function() {
4    // create a new date and insert it
5    var myDate = new Date(2012,11,25,0,0,0)
6    $.countdown('#countbox', myDate);
7});
8
9<h1 id="countbox"></h1>

Just include the JQuery file and call the $.countdown function. The function takes two arguments, a selector and a date object. The returned response will be written into the element you pass in as the selector.

You can view a demo below, or download the whole thing.

JQuery Countdown plugin example

Download the JQuery Countdown plugin

TweetBacks
Comments
andrew's Gravatar Hello Shaun, I'm looking for a simple, leight countdown script. At one page there can be 10-50 instances of countdown displays, of course with different time. As I see, your script can work with this volume od displays but .. But I need a negative time value too. And here is a problem. Generally your script cant work properly with negative values - it not show a correct time.Fex. Today time (2012,03,10,15,00,00), time to countdown (2012,03(02),10,14,00,00) - 1 hour to the past. The display effect? - "-1day -1hour -60 minutes 0 seconds". I think it can be something inside the time match procedures. Can you see at this problem to make your very, very fine countdown clock to work at plus and negative sides? And thanks for your work with this script - it's better than complicated query scripts :))))
# Posted By andrew | 10/03/12 06:08
Shaun McCran's Gravatar Hi Andrew,

Glad you liked the script. I was quite surprised how straight forward I could get things. I think the key here was iteration. I just kept going through the code finessing it each time.

You could two new arguments to the plugin, one could be a direction (up or down) and the other could be an offset, or starting point. In this way you could enable it to not stop at '0' but carry on to wherever you specify.

I'll have a play with it and see what comes up.
# Posted By Shaun McCran | 20/03/12 02:59
Heiko's Gravatar Hey Shaun,
with this great countdownscript for jquery you saved me a lot of time.
I'm just working on a new site and with only a few small modifications it interacts great with a jquery progressbar.

Thanks that you posted it.
# Posted By Heiko | 28/03/12 04:01
Shaun McCran's Gravatar Hi!
Glad that you liked it. Do you have a link to where you have used it?
# Posted By Shaun McCran | 28/03/12 04:05
Heiko's Gravatar Sure you can get the link.
The page is not open for everyone yet so just send me an email and i will send you the link.
I hope it's not a problem that the page is in german.
# Posted By Heiko | 29/03/12 03:58
Shaun McCran's Gravatar Hi,
No thats fine, my German isn't great but I'll drop you an email so you can send me the link through, its always interesting to see how other developers use your examples.
# Posted By Shaun McCran | 01/04/12 14:08
Back to top