Skip to the content...

Welcome to the David Walsh Blog. I'm a MooTools, Dojo, jQuery, CSS, and PHP Web Developer located in Madison, Wisconsin, United States. Please contact me if I can make your experience on my website better.

jQuery Countdown Plugin

21 Responses »

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.

Discussion

  1. December 11, 2008 @ 5:48 pm

    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.

  2. December 11, 2008 @ 6:42 pm

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

  3. December 12, 2008 @ 10:43 am

    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!

  4. vimal
    December 12, 2008 @ 12:33 pm

    Really Nice one. Thanks for sharing.

  5. December 12, 2008 @ 5:58 pm

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

  6. December 14, 2008 @ 11:32 pm

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

  7. February 16, 2009 @ 4:14 pm

    excelente!

  8. April 29, 2009 @ 8:04 am

    nice effect with the font-size :)

  9. ezflash
    June 18, 2009 @ 3:36 pm

    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.

  10. June 18, 2009 @ 3:38 pm

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

  11. ezflash
    June 18, 2009 @ 3:40 pm

    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

  12. September 21, 2009 @ 1:38 pm

    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?

  13. November 10, 2009 @ 11:53 am

    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.

  14. November 12, 2009 @ 3:34 pm

    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;
    }

  15. November 12, 2009 @ 3:35 pm

    And change the text of course:

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

  16. January 22, 2010 @ 1:38 pm

    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);
    }
    }

    });

    });
    };

  17. February 16, 2010 @ 4:57 pm

    how do i do a time extension with this ? as in when users entered a bid or something the time extends directly in the page without refreshing in the explorer?

    i know there is something to do with httprequest ajax but i could not find any help in google or yahoo neither forums anyone can help ?

  18. February 16, 2010 @ 10:11 pm

    i want the time to adjust immediately when the user submits the bid, then just add 15 seconds to the timer after you successfully send the bid via ajax. can anybody help me ?

  19. February 17, 2010 @ 8:05 am

    ok what i have for the code which is the jquery countdown timer.

    I wana make it Extends for the timer for example timer is 30 secs when i click on bid it will extend for another 10 secs into the database and it will update the existing countdown timer to 45 secs automatically

    does anyone knows how to do that ?

  20. arcane
    July 3, 2010 @ 3:54 am

    can we reset the countdown (without reloading)? I can’t figure out a way to stop the original countdown when starting a new one in the same div.

  21. karan
    August 5, 2010 @ 8:21 am

    Hi David,

    I want to show the countdown in following two different formats as below:-

    * min:seconds (Example- 8:29)
    * hour:min:seconds (Example – 1:06:44)

    Please suggest the solution in javascript.

    Thanks

Be Heard!

Share your thoughts with fellow developers of all skill levels! I want to hear from you!

Name*:
Email*:
Website:  
Wrap your code with <code> tags, f00!