Du kan gøre det lidt i stil med dette:
<script type="text/javascript">
/**
* @class Countdown
* @param {string/HTMLElement} el target HTMLElement or id of target HTMLElement.
* @param {string/Date Object} time target time to countdown to.
*/
var CountDown = function(time, cb) {
if(typeof time != "object" || !time.getTime) {
time = new Date(time);
}
this.target = time;
this.cb = cb;
}
CountDown.prototype = {
/**
* Gets distance in seconds
* @return {number}
*/
getDist : function() {
var now = new Date();
var dist = now.getTime() - this.target.getTime();
return dist;
},
/**
* Gets calculated distance
* @return {object}
*/
getCount : function() {
var dist = this.getDist();
var second = 1000
var minute = 60 * second;
var hour = 60 * minute;
var day = 24 * hour;
var days = Math.floor(dist / day);
var hours = Math.floor((dist - (days*day)) / hour);
var minutes = Math.floor((dist - (days*day+hours*hour)) / minute);
var seconds = Math.floor((dist - (days*day+hours*hour+minutes*minute)) / second);
return {
"days" : days, "hours" : hours, "minutes" : minutes, "seconds" : seconds
}
},
isDone : function() {
return dist <= 0 ? true : false;
},
/**
* starts countdown
*/
start : function() {
this.cb.apply(this);
var fn = this._delegate(this.start,this);
this.timer = setTimeout(fn,1000);
},
/**
* stops countdown
*/
stop : function() {
clearTimeout(this.timer);
},
// private
_delegate : function(func,obj) {
return function() {
return func.apply(obj,[]);
}
}
}
/**
* What you need to run it
*/
window.onload = function() {
var cd = new CountDown(new Date("Jan 01 2008 16:00:00"), function() {
if(this.isDone()) {
window.location = 'http://google.com';
}
});
cd.start();
}
</script>
Ovenstående kode er noget jeg har fundet fra et tidl. indlæg jeg skrev:
http://www.udvikleren.dk/forum/23563/nedtaelling/ og herefter tilpasset. Prøv det, eller analyser og brug som det passer dig.