Der er ingen forskel på om du bruger dobbeltfnyf eller enkeltfnyf.
Men jeg fandt problemet i IE, så her er en opdateret udgave:
<div id="test"></div>
<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(el, time) {
if(!el.innerHTML) {
el = document.getElementById(el);
}
this.el = el;
if(typeof time != "object" || !time.getTime) {
time = new Date(time);
}
this.target = time;
}
CountDown.prototype = {
/**
* Gets distance in seconds
* @return {number}
*/
getDist : function() {
var now = new Date();
var dist = now.getTime() - this.target.getTime();
return Math.abs(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
}
},
/**
* starts countdown
*/
start : function() {
var c = this.getCount()
this.el.innerHTML = c.days + 'days '+c.hours+'hours '+c.minutes+'minutes '+c.seconds+'seconds';
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,[]);
}
}
}
window.onload = function() {
var cd = new CountDown("test", new Date("Jan 01 2008 16:00:00"));
cd.start();
}
</script>
Testet og fungerer i IE6+, Firefox2, Opera9.5
Indlæg senest redigeret d. 28.11.2007 21:40 af Bruger #10216