Javascript 如何在每晚16:00重新设置倒计时

Javascript 如何在每晚16:00重新设置倒计时,javascript,php,timer,counter,countdown,Javascript,Php,Timer,Counter,Countdown,我正在为我的网站倒计时。我们在每天16:00前提供当天发货。我需要一个计数器,每天显示16:00的倒计时。 最后,我会修改代码,这样它就不会在周末显示,但现在,我需要的是每天都可以倒计时的东西。16:00后消失,重新开始,从00:00开始重新倒计时 下面是我到目前为止的代码 <?php if (new DateTime() < new DateTime("16:00:00")) { ?> <script type="text/javascript"> v

我正在为我的网站倒计时。我们在每天16:00前提供当天发货。我需要一个计数器,每天显示16:00的倒计时。 最后,我会修改代码,这样它就不会在周末显示,但现在,我需要的是每天都可以倒计时的东西。16:00后消失,重新开始,从00:00开始重新倒计时

下面是我到目前为止的代码

<?php
if (new DateTime() < new DateTime("16:00:00")) {

?>
<script type="text/javascript">

    var CDown = function() {
        this.state=0;// if initialized
        this.counts=[];// array holding countdown date objects and id to print to {d:new Date(2013,11,18,18,54,36), id:"countbox1"}
        this.interval=null;// setInterval object
    }

    CDown.prototype = {
        init: function(){
            this.state=1;
            var self=this;
            this.interval=window.setInterval(function(){self.tick();}, 1000);
        },
        add: function(date,id){
            this.counts.push({d:date,id:id});
            this.tick();
            if(this.state==0) this.init();
        },
        expire: function(idxs){
            for(var x in idxs) {
            this.display(this.counts[idxs[x]], "Now!");
            this.counts.splice(idxs[x], 1);
        }
    },
    format: function(r){
        var out="";
        if(r.d != 0){out += r.d +" "+((r.d==1)?"day":"days")+", ";}
        if(r.h != 0){out += r.h +" "+((r.h==1)?"hour":"hours")+", ";}
        out += r.m +" "+((r.m==1)?"min":"mins")+", ";
        out += r.s +" "+((r.s==1)?"sec":"secs")+", ";

        return out.substr(0,out.length-2);
    },
    math: function(work){
        var y=w=d=h=m=s=ms=0;

        ms=(""+((work%1000)+1000)).substr(1,3);
        work=Math.floor(work/1000);//kill the "milliseconds" so just secs

        y=Math.floor(work/31536000);//years (no leapyear support)
        w=Math.floor(work/604800);//weeks
        d=Math.floor(work/86400);//days
        work=work%86400;

        h=Math.floor(work/3600);//hours
        work=work%3600;

        m=Math.floor(work/60);//minutes
        work=work%60;

        s=Math.floor(work);//seconds

        return {y:y,w:w,d:d,h:h,m:m,s:s,ms:ms};
    },
    tick: function(){
        var now=(new Date()).getTime(),
        expired=[],cnt=0,amount=0;

        if(this.counts)
            for(var idx=0,n=this.counts.length; idx<n; ++idx){
                cnt=this.counts[idx];
                amount=cnt.d.getTime()-now;//calc milliseconds between dates

                // if time is already past
                if(amount<0){
                    expired.push(idx);
                }
                // date is still good
                else{
                    this.display(cnt, this.format(this.math(amount)));
                }
            }

            // deal with any expired
            if(expired.length>0) this.expire(expired);

            // if no active counts, stop updating
            if(this.counts.length==0) window.clearTimeout(this.interval);

        },
        display: function(cnt,msg){
            document.getElementById(cnt.id).innerHTML=msg;
        }
   };

   window.onload=function(){
      var cdown = new CDown();

      cdown.add(new Date(2015,9,16,16,00,00), "countbox1");
   };
 </script>
 <span style="font-size:30px;"><div id="countbox1"></div></span>
 <?php } ?>

var CDown=函数(){
this.state=0;//如果已初始化
this.counts=[];//保存倒计时日期对象和要打印到的id的数组{d:new date(2013,11,18,18,54,36),id:“countbox1”}
this.interval=null;//setInterval对象
}
CDown.prototype={
init:function(){
该状态=1;
var self=这个;
this.interval=window.setInterval(函数(){self.tick();},1000);
},
添加:函数(日期、id){
this.counts.push({d:date,id:id});
这个。勾选();
如果(this.state==0)this.init();
},
过期:函数(idxs){
for(idxs中的变量x){
this.display(this.counts[idxs[x]],“现在!”);
这个.counts.splice(idxs[x],1);
}
},
格式:函数(r){
var out=“”;
如果(r.d!=0){out+=r.d++++((r.d==1)?“天”:“天”)+“,;}
如果(r.h!=0){out+=r.h++++((r.h==1)?“小时”:“小时”)+,“;}
out+=r.m++((r.m==1)?“min”:“min”)+“,”;
out+=r.s++((r.s==1)?“秒”:“秒”)+“,”;
返回out.substr(0,out.length-2);
},
数学:函数(功){
变量y=w=d=h=m=s=ms=0;
ms=(“”+((工作%1000)+1000)).substr(1,3);
work=Math.floor(work/1000);//去掉“毫秒”,只需几秒钟
y=Math.floor(工作/31536000);//年(无leapyear支持)
w=数学地板(工作/604800);//周
d=数学地板(工作/86400);//天
工时=工时%86400;
h=数学楼层(工作/3600);//小时
工时=工时%3600;
m=数学地板(工作/60);//分钟
工时=工时%60;
s=数学。地板(工作);//秒
返回{y:y,w:w,d:d,h:h,m:m,s:s,ms:ms};
},
勾选:函数(){
var now=(新日期()).getTime(),
过期=[],cnt=0,金额=0;
如果(这一点很重要)
对于(var idx=0,n=this.counts.length;idx您可以使用将每24小时重置一次变量的
将变量倒计时更改为第8、103和109行的当前小时
在第133行更改变量分钟


var检查=0;
/*设置倒计时时间*/
var倒计时=16;
var CDown=函数(){
this.state=0;//如果已初始化
this.counts=[];//包含倒计时日期对象和要打印到的id的数组{d:new date(2013,11,18,18,54,36),id:“countbox1”}
this.interval=null;//setInterval对象
}
CDown.prototype={
init:function(){
该状态=1;
var self=这个;
this.interval=window.setInterval(函数(){
self.tick();
}, 1000);
},
添加:函数(日期、id){
这是推({
d:日期,
id:id
});
这个。勾选();
如果(this.state==0)this.init();
},
过期:函数(idxs){
for(idxs中的变量x){
this.display(this.counts[idxs[x]],“现在!”);
这个.counts.splice(idxs[x],1);
}
},
格式:函数(r){
var out=“”;
如果(r.d!=0){
out+=r.d++((r.d==1)?“天”:“天”)+;
}
如果(r.h!=0){
out+=r.h++((r.h==1)?“小时”:“小时”)+;
}
out+=r.m++((r.m==1)?“min”:“min”)+“,”;
out+=r.s++((r.s==1)?“秒”:“秒”)+“,”;
return out.substr(0,out.length-2);
},
数学:函数(功){
变量y=w=d=h=m=s=ms=0;
ms=(“”+((工作%1000)+1000)).substr(1,3);
work=Math.floor(work/1000);//去掉“毫秒”,只需几秒钟
y=Math.floor(工作/31536000);//年(无leapyear支持)
w=数学地板(工作/604800);//周
d=数学地板(工作/86400);//天
工时=工时%86400;
h=数学楼层(工作/3600);//小时
工时=工时%3600;
m=数学地板(工作/60);//分钟
工时=工时%60;
s=数学。地板(工作);//秒
返回{
y:y,
w:w,
d:d,
h:h,
m:m,
s:s,
小姐:小姐
};
},
勾选:函数(){
var now=(新日期()).getTime(),
过期=[],
cnt=0,
金额=0;
如果(这一点很重要)
对于(var idx=0,n=this.counts.length;idx0),则此.expire(expired);
//如果没有活动计数,请停止更新
if(this.counts.length==0)window.clearTimeout(this.interval);
},
显示:功能(cnt、msg){
如果(msg=='Now!`){
检查=1;
味精=``;
var cdown=new cdown();
var currentdate=新日期();
var year=currentdate.getFullYear();
var month=currentdate.getMonth();
var day=currentdate.getDate()+1;
var currenthour=currentdate.getHours();
/*在这里执行检查*/
如果(倒计时==16){
倒计时=0;
}否则{
倒计时=16;
}
var小时=倒计时;
var分钟=0;
var秒=0;
添加(新日期(年、月、日、时、分、秒),“countbox1”);
}否则{
检查=0;
}