Liberamente tratto da qui... http://forum.html.it/forum/showthrea...2#post13813382:
codice:
<!doctype html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>CountDown</title>
<script type="text/javascript">
function Timer (oNode, sGMTDate) {
if (arguments.length < 2) { throw new TypeError("Timer - not enough arguments"); }
this.expires = Date.parse(sGMTDate) || 0x1f3fffffc18;
this.owner = oNode;
Timer.instances.push(this);
}
Timer.refreshAll = function () {
for (var nDelta, nDLeft, nHLeft, nMLeft, nSLeft, oThis, oDisplay, nInst = 0; nInst < Timer.instances.length; nInst++) {
oThis = Timer.instances[nInst];
if (!oThis) { continue; }
nDelta = oThis.expires - Date.now();
/* Casual expiration test */
// if (Math.floor(Math.random() * 8) === 0) { nDelta = 0; }
if (nDelta <= 0) {
Timer.instances[nInst] = null;
oThis.owner.innerHTML = "Tempo scaduto";
continue;
}
nDLeft = Math.floor(nDelta / 864e5);
nDelta -= (nDLeft * 864e5);
nHLeft = Math.floor(nDelta / 36e5);
nDelta -= (nHLeft * 36e5);
nMLeft = Math.floor(nDelta / 6e4);
nDelta -= (nMLeft * 6e4);
nSLeft = Math.floor(nDelta / 1e3);
nDelta -= (nSLeft * 1e3);
oThis.owner.innerHTML = nDLeft + " giorni, " + nHLeft + " ore, " + nMLeft + " minuti e " + nSLeft + " secondi";
}
};
Timer.instances = [];
Timer.session = setInterval(Timer.refreshAll, 1000);
onload = function () {
for (var aTimers = document.getElementsByClassName("countdown"), oTimer, nLen = aTimers.length, nItem = 0; nItem < nLen; nItem++) {
oTimer = aTimers[nItem];
if (!oTimer.hasAttribute("data-expires")) { continue; }
new Timer(oTimer, oTimer.getAttribute("data-expires"));
}
Timer.refreshAll();
};
</script>
</head>
<body>
<div class="countdown" data-expires="Thu, 10 Jun 2088 22:36:43 GMT"></div>
<div class="countdown" data-expires="Sat, 22 Apr 2215 05:15:19 GMT"></div>
<div class="countdown" data-expires="Sun, 12 Jan 2251 08:22:20 GMT"></div>
</body>
</html>