解决用户手动修改本地时间的bug
<div class="free_time" id="clock"> <cite class="fr mts f14"><strong>还剩<font class="c_pink" id="surplus">10</font>份</strong></cite> <cite class="c_grey2 f14">剩余时间:</cite> <span class="time_num">00</span>天 <span class="time_num">00</span>小时 <span class="time_num">00</span>分 <span class="time_num">00.0</span>秒 </div> Clock.startCount({ target: clockBox, endTime:1337826623, nowTime:1337587763 }); /** * @version 2012.05.21 * @author lq **/ var Clock = (function(){ var GetTime = function(o){ this.setting = typeof o === 'object' ? o : {}; this.target = this.setting.target || 'clock'; this.elems = this.target.getElementsByTagName("span"); this.nowTimes = this.setting.nowTime || 0; this.endTimes = this.setting.endTime || 0; //控制器 this.controller; this.setTimes = this.setting.seTimes || 100; this.nowTime= new Date(parseInt(this.nowTimes)*1000); this.endTime= new Date(parseInt(this.endTimes)*1000); //初始化时间 this.day = 0; this.hour = 0; this.minute = 0; this.second = 0; this.Ms = 0; this.init(); } GetTime.prototype ={ init:function(){ var me = this; var time = me.endTime.getTime() - me.nowTime.getTime(); me.nowTime = new Date(me.nowTime.getTime() + me.setTimes); if(time!=0 || time != null){ if(parseInt(time)&&time>=0&&time!=""){ //初始化 me.showTime(time); me.controller = setInterval(function(){ me.setTimeCount(); },me.setTimes); }else{ me.elemCreat(); } } }, setTimeCount:function(){ var me = this; var newTime = me.nowTime; var time = me.endTime; if(newTime<=0){ me.statusFn(); clearTimeout(me.controller); return; } var now = time.getTime() - this.nowTime.getTime(); this.nowTime = now<=0 ? 0 : new Date(this.nowTime.getTime() + me.setTimes); me.showTime(now); }, showTime:function(time){ this.day = Math.floor(time/(1000 * 60 * 60 * 24)); this.hour = Math.floor(time/(1000*60*60)) % 24; this.minute = Math.floor(time/(1000*60)) % 60; this.second = Math.floor(time/1000) % 60; this.Ms = Math.floor(time/100) % 10; // this.elemCreat(); }, elemCreat:function(){ var me = this; var second,minute,hour,day; if(me.Ms<=0){me.Ms=0;} second = me.second<=0 ? "00" : me.second; minute = me.minute<=0 ? "00" : me.minute; hour = me.hour<=0 ? "00" : me.hour; day = me.day<=0 ? "00" : me.day; // me.elems[3].innerHTML = second+"."+me.Ms; me.elems[2].innerHTML = minute; me.elems[1].innerHTML = hour; me.elems[0].innerHTML = day; } } return { startCount:function(o){ var _time = new GetTime(o); } } })();