jQuery Countdown Plugin

Written by David Walsh on Thursday, December 11, 2008


You’ve probably been to sites like RapidShare and MegaUpload that allow you to download files but make you wait a specified number of seconds before giving you the download link. I’ve created a similar script but my script allows you to animate the CSS font-size of each second and present a reward at the end.

The jQuery Javascript

jQuery.fn.countDown = function(settings,to) {
	settings = jQuery.extend({
		startFontSize: '36px',
		endFontSize: '12px',
		duration: 1000,
		startNumber: 10,
		endNumber: 0,
		callBack: function() { }
	}, settings);
	return this.each(function() {
		
		//where do we start?
		if(!to && to != settings.endNumber) { to = settings.startNumber; }
		
		//set the countdown to the starting number
		$(this).text(to).css('fontSize',settings.startFontSize);
		
		//loopage
		$(this).animate({
			'fontSize': settings.endFontSize
		},settings.duration,'',function() {
			if(to > settings.endNumber + 1) {
				$(this).css('fontSize',settings.startFontSize).text(to - 1).countDown(settings,to - 1);
			}
			else
			{
				settings.callBack(this);
			}
		});
				
	});
};

Sample Usage

$('#countdown').countDown({
	startNumber: 10,
	callBack: function(me) {
		$(me).text('All done! This is where you give the reward!').css('color','#090');
	}
});

The script is very customizable and the settings are self-explanatory.

Check out the MooTools version.


Follow via RSS Epic Discussion

Commenter Avatar December 11 / #

That’s funny, I did basically the same yesterday, only using PHP and CSS though. You can see my randomly styled PHP countdown here. Great blog you have.

Commenter Avatar December 11 / #
Patareco says:

Nice! I like this kind of stuff! Love your snippets

Commenter Avatar December 12 / #
Stefan says:

Really nice plugin…

But to be hones, I though it’s about a plugin counting down the time until a specific event… I am looking for a jquery-thing counting back the time until a specific date… Nevertheless => cool!

@Dirk – looks reallyreally great!

Commenter Avatar December 12 / #
Vimal says:

Really Nice one. Thanks for sharing.

Commenter Avatar December 12 / #

Cool snippet! Made this myself last week but this one is much better! Thanks man!

Commenter Avatar December 14 / #

very cool script. I’d like apply it on my website

Commenter Avatar February 16 / #
Janckos says:

excelente!

Commenter Avatar April 29 / #
agentor says:

nice effect with the font-size :)

Commenter Avatar June 18 / #
ezFlash says:

these snippets are all cool but they never seem to work for me. Its so frustrating. I have the countDown.js file linked with the sample code, altered to have my div class in there…and nothing. dammit.

David Walsh June 18 / #

ezFlash: No offense, but your comment was useless. How about providing a link to your code so we can help?

Commenter Avatar June 18 / #
ezFlash says:

I copied the source and it worked. Thanks :)
Maybe its something with the linked file that I’m doing wrong or the placement of the sample usage vs. the link…. I don’t know. anyway thanks again

ezFlash

Commenter Avatar September 21 / #
orves says:

Hi all,

Nice script I only have one question; I would like to use this script to countdown from let’s say 5 minutes and then repeat that. Hoe can I archieve this?

Commenter Avatar November 10 / #

Thanks for this demo. I love the idea but I wanted to know if you could show how to alter this code so that it becomes a date countdown? I am looking for countdown to tax day.

Commenter Avatar November 12 / #

Thanks for the plugin.
Here is a small modification for those who want to show time instead of minutes

//where do we start?
if(!to && to != settings.endNumber) { to = settings.startNumber; }

hours = Math.floor(to / 60);
minutes = Math.round(to % 60);
if (minutes < 10) {
minutes = "0"+minutes;
}

Commenter Avatar November 12 / #

And change the text of course:

//set the countdown to the starting number
$(this).text(hours + ‘:’ + minutes).css(‘fontSize’,settings.startFontSize);

Commenter Avatar January 22 / #

Try this, if you maybe need to have a choice on the direction it is counting:

jQuery.fn.countDown = function(settings,to) {
settings = jQuery.extend({
startFontSize: ‘12px’,
endFontSize: ‘12px’,
duration: 1000,
direction: ‘down’,
startNumber: 10,
endNumber: 0,
callBack: function() { }
}, settings);
return this.each(function() {

//where do we start?
if(!to && to != settings.endNumber) { to = settings.startNumber; }

//set the countdown to the starting number
$(this).text(to).css(‘fontSize’,settings.startFontSize);

//loopage
$(this).animate({
‘fontSize’: settings.endFontSize
},settings.duration,”,function() {

if(settings.direction == ‘down’){
if(to > settings.endNumber + 1) {
$(this).css(‘fontSize’,settings.startFontSize).text(to – 1).countDown(settings,to – 1);
}
else{
settings.callBack(this);
}
}

if(settings.direction == ‘up’) {
if(to < settings.endNumber + 1) {
$(this).css('fontSize',settings.startFontSize).text(to + 1).countDown(settings,to + 1);
}
else{
settings.callBack(this);
}
}

});

});
};

Be Heard!

I want to hear what you have to say! Share your comments and questions below.

Name*:
Email*:
Website:  


© David Walsh 2007-2010. Contact David Walsh. Powered by the remarkable MooTools javascript framework.