和上一篇实现的效果是一样的 就是以闭包的方式实现了下,看着和用着跟舒服一些

<div id="countdown" class="timer">
    <span class="days"></span><span class="hour"></span><span class="minute"></span><span class="second"></span></div>
<div id="countdown2" class="timer">
    <span id="days"></span><span id="hour"></span><span id="minute"></span><span id="second"></span></div>

<script type="text/javascript">
window.onload = function(){
    //实例化
    var oDiv = document.getElementById("countdown");
       Clock.startCount({
         target: oDiv,
         endTime:1337508688400
    });
    Clock.startCount({
         target: document.getElementById("countdown2"),
         endTime:1337308658400
    });    
}
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.endTime = this.setting.endTime || 0;  
        //控制器
        this.controller;
        this.setTimes = this.setting.seTimes || 100;
        this.endTime= new Date(this.endTime);
        //初始化时间
        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 NowTime = new Date();
            var time = me.endTime.getTime() - NowTime.getTime();
            if(time!=0 || time != null){
                if(parseInt(time) &&time>=0 ){
                    //初始化
                    me.showTime(time);    
                    me.controller = setInterval(function(){
                        me.setTimeCount();
                    },me.setTimes);
                }else{
                    me.elemCreat();
                }
            }
        },
        setTimeCount:function(){
            var me = this;
            var time = me.endTime;
            if(time<=0){
                clearTimeout(me.controller);
                return;    
            }
            var NowTime = new Date();
            var now = time.getTime() - NowTime.getTime();
            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);
        }    
    }
})();
</script>

 

 

 posted on 2012-05-17 19:59  刘卿  阅读(189)  评论(0编辑  收藏  举报