mercoledì 20 febbraio 2013

Simple javascript countdown script

I've created for a mine project a simple countdown script in JS, I think it's easy to use and really flexible.
Here is the code:
function countdown() {
    var yourdate = new Date("April 12, 2013 9:30:00"); 

    var now = new Date();
    var timeDifference = yourdate.getTime() - now.getTime();
    if (timeDifference <= 0) {
        //any other code to run on countdown completition
    var seconds = Math.floor(timeDifference / 1000);
    var minutes = Math.floor(seconds / 60);
    var hours = Math.floor(minutes / 60);
    var days = Math.floor(hours / 24);
    hours %= 24;
    minutes %= 60;
    seconds %= 60;
    document.getElementById("days").innerHTML = days;
    document.getElementById("hours").innerHTML = hours;
    document.getElementById("minutes").innerHTML =  minutes;
    document.getElementById("seconds").innerHTML = seconds;
    var timer = setTimeout('countdown()',1000);
text-align: center;
#countdown li{
display: inline;
list-style-type: none;
padding-right: 10px;
font-size: 60px;
background: #1560BD;
<body onload="countdown()">
<div id="cdcontainer">
<ul id="countdown">
<li id="days"></li>
<li id="hours"></li>
<li id="minutes"></li>
<li id="seconds"></li>

This code is shared in the same license of the blog (creative commons, for more info check the end of the page)
I  hope this simple script will help you, for any information comment or send me an e-mail to

Bye, Dami

Nessun commento:

Posta un commento