Jump to content
MakeWebGames

jQuery Second Minute Countdown.


Dave

Recommended Posts

Well basically I struggled to find a decent javascript/jQuery countdown timer so I created a working yet most likely poor version to use myself. It's pretty simple and only currently supports minutes and seconds.

function countdown() {
var m = $('.min');
var s = $('.sec');	
if(m.length == 0 && parseInt(s.html()) <= 0) {
	$('.clock').html('Timer Complete.');	
}
if(parseInt(s.html()) <= 0) {
	m.html(parseInt(m.html()-1));	
	s.html(60);
}
if(parseInt(m.html()) <= 0) {
	$('.clock').html('<span class="sec">59</span> seconds.');	
}
s.html(parseInt(s.html()-1));
}
setInterval('countdown()',1000);
Then you have to structure the "timer" area like such.
<div class="clock">
<span class="min">6</span> minutes <span class="sec">15</span> seconds.
</div>
If your site supports timestamps this could be a nice small addition to make all the timers dynamic if you already haven't. I'd like to remind you this code is not perfect but it does it's job and works.

There are issues with multiple timers on a page at the moment.

Link to comment
Share on other sites

Danny, it wouldn't work well with val() the way Dave is doing it.

Okay so for multiple timers we can do it like this;

[js]

$(document).ready (function () {

$('.clock').each(function () {

var clock = $(this);

setInterval(function () {

var m = $('.min', clock),

s = $('.sec', clock);

if (m.length == 0 && parseInt(s.html()) <= 0)

clock.html('Timer Complete.');

if (parseInt(s.html()) <= 0) {

m.html(parseInt(m.html() - 1));

s.html(60);

}

if (parseInt(m.html()) <= 0)

clock.html('<span class="sec">59</span> seconds.');

s.html(parseInt(s.html() -1));

}, 1000);

});

});

[/js]

HTML would be something along the lines of;

<p class="clock">
<span class="min">7</span> minutes and <span class="sec">30</span> seconds
</p>
<p class="clock">
<span class="min">3</span> minutes and <span class="sec">20</span> seconds
</p>

Bah code tags really don't like my 'tab' tabbing but meh.

Link to comment
Share on other sites

I got rather bored, so I did a quick and dirty (very) plugin for this, also allows hours

[js]

(function($){

$.fn.countdown = function (options) {

options = $.extend({

start : '1,1,1',

countdownTitle: 'Till the end of the world',

countdownCompleteMsg: 'Timer has completed.'

}, options);

return this.each(function () {

var unit = options.start.split(','),

hour = unit[0],

min = unit[1],

sec = unit[2];

html = [

'<p class="clock">',

'<span class="hour">', hour,'</span> Hours ',

'<span class="min">', min,'</span> Minutes ',

'<span class="sec">', sec,'</span> Seconds ',

'<span>', options.countdownTitle,'</span>',

'</p>'

].join('');

$(html).appendTo(this);

var clock = $(this);

setInterval(function () {

var h = $('.hour', clock);

m = $('.min', clock),

s = $('.sec', clock);

if (parseInt(h.html()) <= 0 && parseInt(m.html()) > 0) {

clock.html('<span class="min">'+m.html()+'</span> Minutes <span class="sec">'+s.html()+'</span> seconds '+options.countdownTitle);

} else if (parseInt(h.html()) <= 0 && parseInt(m.html()) <= 0) {

clock.html('<span class="sec">'+s.html()+'</span> seconds ' + options.countdownTitle);

}

if (parseInt(h.html()) > 0 && parseInt(m.html()) <= 0 && parseInt(s.html()) <= 0) {

h.html(h.html() -1);

m.html(59);

s.html(59);

}

if (parseInt(m.html()) > 0 && parseInt(s.html()) <= 0) {

m.html(m.html() -1);

s.html(59);

}

if (h.length == 0 && m.length == 0 && parseInt(s.html()) == 0)

clock.html(options.countdownCompleteMsg);

else

s.html(parseInt(s.html() -1));

}, 1000);

});

}

})(jQuery);[/js]

Usage

[js]

$(elem).countdown(); //Will apply the default settings

$(elem).countdown({

start: '2,30,30'

}); //Will start the countdown at 2 hours 30 minutes and 30 seconds.

$(elem).countdown({

start: '0,40,0'

}); // Will start the countdown at 40 minutes.

//There's two other options 'countdownTitle' and 'countdownCompleteMsg'

//countdownTitle is what appears after the countdown, using the default as an example "10 seconds till the end of the world"

//countdownCompleteMsg is what appears when every thing hits 0, default is "Timer has completed".

[/js]

I may rework it and have separate options for hour/min/sec rather than split them, and clean it up a little and possibly have an option for transitions (fadeIn/Out etc).

It has the ability to have different counters, and also the same counter in different places (by adding the same classed element in different places);

Link to comment
Share on other sites

  • 1 year later...

jQuery game countdown

Hi,

sorry for pop-up this theme after a year, I got finsh my jQuery game countdown, for those who need it:

https://github.com/Harveyhase68/jQuery.game.countdown or http://www.predl.cc (german)

Makes multiple timers with one timer (so website won't slow down), start/set/remove, tested with jQuery 1.8.2, simple display of 1d 1h 1m 1s, exact countdown with date, not with timeout 1000 = 1 sec, and of course gpl!

Any comments and issues appreciated!

yours

Alex

Link to comment
Share on other sites

Hi,

sorry for pop-up this theme after a year, I got finsh my jQuery game countdown, for those who need it:

https://github.com/Harveyhase68/jQuery.game.countdown or http://www.predl.cc (german)

Makes multiple timers with one timer (so website won't slow down), start/set/remove, tested with jQuery 1.8.2, simple display of 1d 1h 1m 1s, exact countdown with date, not with timeout 1000 = 1 sec, and of course gpl!

Any comments and issues appreciated!

yours

Alex

Nice one, good job.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...