Earth from space

Javascript countdown

So I’ve created this little countdown tool.

This will count down to a specific date and time… and then display a specific message during and then after the specified time.

The tool being used to countdown to Earth Hour on the WWF website
The tool being used to countdown to Earth Hour on the WWF website

It’s pretty lightweight (and relies on JQuery – again)

The Countdown HTML:

<span class="lineOne">This is where the countdown goes </span>
<span class="subLine">until the event</span>

The Countdown Javascript:

You’ll see on line 8 and 9 there are timestamps – these are for the beginning and the end of the event – you’ll have to figure out what the timestamps for these dates and times are.

Line 44 and 49 are the messages that will come up during and after the event.

$(document).ready(function(){
		console.log("ping");
		var 	countSpeed = 2,
			dayLength = 86400000,
			hourLength = 3600000,
			minLength = 60000,
			secondLength = 1000,
			eventStamp = 1396125000000,
			eventOverStamp = 1396128600000;
			setInterval(countUp,1000 / countSpeed);

		function countUp(){

			var 	thisNow = jQuery.now(),
				eventDiff = eventStamp - thisNow,
				daysLeft = eventDiff / dayLength,
				daysLeft = Math.floor(daysLeft),
				hoursLeft = (eventDiff - (daysLeft * dayLength)) / hourLength,
				hoursLeft = Math.floor(hoursLeft),
				minsLeft = (eventDiff - (daysLeft * dayLength) - (hoursLeft * hourLength)) / minLength,
				minsLeft = Math.floor(minsLeft),
				secsLeft = (eventDiff - (daysLeft * dayLength) - (hoursLeft * hourLength) - (minsLeft * minLength)) / secondLength,
				secsLeft = Math.floor(secsLeft);
				millisLeft = eventDiff - (daysLeft * dayLength) - (hoursLeft * hourLength) - (minsLeft * minLength) - (secsLeft * secondLength);
				if(millisLeft > 99) { 
					millisLeft = ""+millisLeft;
					millisLeft = millisLeft.substring(0, millisLeft.length - 1);
				}
				if(secsLeft<10){ secsLeft = "0" + secsLeft; }
				if(minsLeft<10){ minsLeft = "0" + minsLeft; }
				if(hoursLeft<10){ hoursLeft = "0" + hoursLeft; }
				if(daysLeft<10){ daysLeft = "0" + daysLeft; }

			$(".lineOne").empty().append("00 days 00 hours 00 minutes 00 seconds ");
			$("#days").empty().append(daysLeft);
			$("#hours").empty().append(hoursLeft);
			$("#minutes").empty().append(minsLeft);
			$("#seconds").empty().append(secsLeft);
			$("#millis").empty().append(millisLeft);

			if(thisNow > eventStamp && thisNow < eventOverStamp){ 				$(".lineOne").empty().append("Earth Hour"); 				$(".subLine").empty().append("is currently underway").css("margin-top", "180px"); 				$("#heroSpace .wwf_button").empty().append("Find out what's happening"); 			} 			else if (thisNow > eventOverStamp){
				$(".lineOne").empty().append("Earth Hour");
				$(".subLine").empty().append("has been and gone").css("margin-top", "180px");
				$("#heroSpace .wwf_button").empty().append("Find out what happened");
			}

		};

	})

Obviously – please feel free to use as you wish. Let me know how you’ve found it in the comments.

Leave a Reply

Your email address will not be published. Required fields are marked *