2015-10-16 83 views
0

我正在爲我的網站倒計時工作。我們在每天16:00之前提供當天發貨。我需要一個計數器,每天倒數到16點。最終,我會修改代碼,使其在週末根本不顯示,但現在,我需要的是每天都可以倒計時的東西。 16:00後消失,並從00:00開始重新開始倒計時如何在16: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 } ?> 
+0

我很抱歉,我不能格式這個正確的,我也不認爲它已經粘貼了所有的。我將這個文件放在一個在線文件中並快速分享 –

+0

你的問題在於你必須一直保持打開特定的瀏覽器標籤,因爲如果有人關閉瀏覽器等時間間隔將停止計數。我通常使用一個批次文件重新打開頁面,然後使用標準任務計劃程序每天運行批處理文件。 – Shilly

+0

爲了確保網頁訪問者看到正確的時間,請使用PHP來計算剩餘時間,並將其提供給JavaScript代碼(作爲函數參數)。否則,訪客將根據他們的本地機器時鐘(可能與您的公司時鐘不同幾分鐘或幾小時)看到一個時間。如果你這樣做,確保頁面不會被緩存,否則訪問者會看到一個過時的剩餘時間。 – Bobulous

回答

-2

您可以使用一個cron job將每24小時重置一次變量。

+0

我真的不知道從哪裏開始 –

+0

PHP和JavaScript都可用,cron作業和系統變量對於此目的完全沒有必要。 – Bobulous

+0

該腳本可以控制此功能,並且不需要cron作業 –

0

概念在這裏測試 變量countDownTo更改爲當前小時線8,103和109 變化對行的變量分鐘133

<?php if (new DateTime() < new DateTime("16:00:00")) { ?> 
 
<script type="text/javascript"> 
 
    var check = 0; 
 
    /*set the countdown hour*/ 
 
    var countDownTo = 16; 
 
    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) { 
 
     if (msg == `Now!`) { 
 
     check = 1; 
 
     msg = ``; 
 
     var cdown = new CDown(); 
 
     var currentdate = new Date(); 
 
     var year = currentdate.getFullYear(); 
 
     var month = currentdate.getMonth(); 
 
     var day = currentdate.getDate() + 1; 
 
     var currenthour = currentdate.getHours(); 
 
     /*perform check here*/ 
 
     if (countDownTo == 16) { 
 
      countDownTo = 0; 
 
     } else { 
 
      countDownTo = 16; 
 
     } 
 
     var hour = countDownTo; 
 
     var minute = 0; 
 
     var second = 0; 
 
     cdown.add(new Date(year, month, day, hour, minute, second), "countbox1"); 
 
     } else { 
 
     check = 0; 
 
     } 
 
     if (countDownTo == 0) msg = ``; 
 
     document.getElementById(cnt.id).innerHTML = msg; 
 

 

 
    } 
 
    }; 
 

 
    window.onload = function() { 
 
    var cdown = new CDown(); 
 
    var currentdate = new Date(); 
 
    var year = currentdate.getFullYear(); 
 
    var month = currentdate.getMonth(); 
 
    var day = currentdate.getDate(); 
 
    var hour = countDownTo; 
 
    var minute = 0; 
 
    var second = 0; 
 
    cdown.add(new Date(year, month, day, hour, minute, second), "countbox1"); 
 
    }; 
 
</script> 
 
<span style="font-size:30px;"><div id="countbox1"></div></span> 
 
<?php } ?>