js的倒计时 服务器时间与本地时间不一致

时间:2022-01-05 17:09:26

一、单个的倒计时

// 设置开始时间,注意时间格式
        var setstartTime="2015/07/30 15:00:00";
        // 设置结束时间,注意时间格式
        var setendTime="2015/07/31 19:00:00";

         var EndTime=new Date(setendTime);
         var StartTime=new Date(setstartTime);
         // 预热时间
         var warm_up=new Date(StartTime.getFullYear()+"/"+(StartTime.getMonth()+1)+"/"+StartTime.getDate()+" 00:00:00");
         var nMS;
        function timer(){
                //时间默认值 
                var day=0,
                    hour=0,
                    minute=0,
                    second=0;  
                var NowTime = new Date();
                if(NowTime.getTime()<warm_up){
                    // 活动预热未开始或没有活动
                    sTodayText = '暂无活动';
                    hour = 0;  
                    minute = 0;  
                    second = 0;
                }else if(NowTime.getTime()>warm_up && NowTime.getTime()<StartTime){
                    sTodayText = '距离秒杀活动开始还有';
                    nMS = StartTime - NowTime.getTime();
                    hour = Math.floor(nMS/(1000*60*60));  
                    minute = Math.floor(nMS/(1000*60)) % 60;  
                    second = Math.floor(nMS/1000) % 60;
                }else if(NowTime.getTime()>StartTime && NowTime.getTime()<EndTime){
                    sTodayText = '距离本次秒杀活动结束还有'; 
                    nMS = EndTime - NowTime.getTime();
                    hour = Math.floor(nMS/(1000*60*60));  
                    minute = Math.floor(nMS/(1000*60)) % 60;  
                    second = Math.floor(nMS/1000) % 60;
                }else if(NowTime.getTime()>EndTime){
                    sTodayText = '秒杀活动已结束';
                    hour = 0;  
                    minute = 0;  
                    second = 0;
                    clearInterval(setST);
                }
                 jQuery('.xi-time .hour').text(formatDate(hour));
                 jQuery('.xi-time .minute').text(formatDate(minute));
                 jQuery('.xi-time .second').text(formatDate(second));
                 jQuery(".xi-box .xi-hd h3").html(sTodayText);
        }
</pre><pre name="code" class="javascript">// 格式化时间
    function formatDate(oDate) {
        oDate=parseInt(oDate)
        oDate = oDate< 10 ? '0'+oDate : oDate;
        return (oDate);
    }


二、多个倒计时   拷贝的


<body>  
    <div id="timer1">  
    </div>  
    <div id="timer2">  
    </div>  
    <div id="timer3">  
    </div>  
</body>
<script type="text/javascript">
    var addTimer = function () {     
        var list = [],     
            interval;
        return function (id, time) {     
            if (!interval)     
                interval = setInterval(go, 1000);     
            list.push({ ele: document.getElementById(id), time: time });     
        }     
    
        function go() {     
            for (var i = 0; i < list.length; i++) {     
                list[i].ele.innerHTML = getTimerString(list[i].time ? list[i].time -= 1 : 0);     
                if (!list[i].time)     
                    list.splice(i--, 1);     
            }     
        }     
    
        function getTimerString(time) {     
                d = Math.floor(time / 86400),     
                h = Math.floor((time % 86400) / 3600),     
                m = Math.floor(((time % 86400) % 3600) / 60),     
                s = Math.floor(((time % 86400) % 3600) % 60);     
            if (time>0)     
                return d + "天" + h + "小时" + m + "分" + s + "秒";       
            else return "时间到";     
        }     
    } ();
	
    addTimer("timer1", 99999);     
    addTimer("timer2", 66666);     
    addTimer("timer3", 33333);   
</script> 


PS:解决本地时间与服务器时间不一致问题

var NowTime = (服务器时间-本地时间)+new Date();