2012-08-10 73 views
1

我試圖從數據庫創建倒計時器。我已將deltaTimeServer發送給JS。輸出是正確的,但他們凍結(不倒計時,我必須按F5)。對我有什麼想法?從mysql的倒​​計時器定時器

這是我的代碼。

JS

<script type="text/javascript"> 
function countDown(){ 
    $(".show").each(function() { 
      var elm = $(this); 
      var difTime=this.timestamp; 
      var day=0,hours=0,minutes=0,seconds=0; 
      if(difTime>0){ 
       day=Math.floor(difTime/84600); 
       hours=(Math.floor((difTime/3600))%24) + day*24 ; 
       minutes=Math.floor(difTime/60)%60; 
       seconds=Math.floor(difTime)%60; 
      } 
      else{ 
       elm.removeClass("show"); //for remove class show 
      } 
      elm.html(hours+' H '+minutes+' M '+seconds+' S '); 
     }); 
} 
function countDown_onLoad(){ 
     $(".show").each(function() { 
      this.timestamp = parseInt(this.firstChild.nodeValue,10); 
     }); 
     setInterval(countDown,1000); 
    } 
    $(document).ready(function() { 
     countDown_onLoad(); 
    }); 
</script> 

PHP

$show=mysql_query("SELECT * FROM `room_lists` WHERE `active` = 1"); 
while ($array = mysql_fetch_array($show)) 
{ 
    $timeStop = $array['timeStop']; 
    $deltaTimeServer = strtotime($timeStop)-strtotime(date('Y-m-d H:i:s')); 
    echo "<td align = 'center'><div class=\"show\">".$deltaTimeServer."</div></td>"; 
} 
+0

檢查您的瀏覽器控制檯,可能是一個流氓例外拋出'setInterval' – nbaztec 2012-08-10 06:59:49

回答

0

你JS中的問題是你的difTime總是一樣的。您必須從原始this.timestamp減去當前時間戳。

但是,因爲你每秒都在倒計時,所以更容易。你可以減少時間戳:

var difTime=this.timestamp--; 

而且應該這樣做。

此外,您還可以改變這一點:

$deltaTimeServer = strtotime($timeStop)-strtotime(date('Y-m-d H:i:s')); 

要這樣:

$deltaTimeServer = strtotime($timeStop)-time(); 

最後,我想對tdstyle="display:none"隱藏在你輸出了它在PHP。一旦設置了時間戳,您可以清除td並顯示它 - 這樣人們就看不到一個變成計數器的時間戳。

+0

它的工作很棒。 謝謝:) – 2012-08-10 08:33:16

0

你應該setInterval的獲得功能新的時間戳。