2016-09-05 143 views
2

我正在做一個「倒計時」,但是當它達到0時,它計爲負數; -1 -2等。我要讓我停在0jquery使倒計時

我:「天,時,分,秒」

當一切都爲00:00:00:00我希望它停止。

幫助:d

<script src="//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"> </script> 
    <script type="text/javascript"> 
    $(document).ready(function(){ 

    function makeTimer() { 

    var endTime = new Date("September 5, 2016 10:50:50 UTC+02:00"); 
    var endTime = (Date.parse(endTime))/1000; 

    var now = new Date(); 
    var now = (Date.parse(now)/1000); 

    var timeLeft = endTime - now; 

    var days = Math.floor(timeLeft/86400); 
    var hours = Math.floor((timeLeft - (days * 86400))/3600); 
    var minutes = Math.floor((timeLeft - (days * 86400) - (hours * 3600))/60); 
    var seconds = Math.floor((timeLeft - (days * 86400) - (hours * 3600) - (minutes * 60))); 

    if (hours < "10") { hours = "0" + hours; } 
    if (minutes < "10") { minutes = "0" + minutes; } 
    if (seconds < "10") { seconds = "0" + seconds; }  

    $("#days").html(days + "<span>Days</span>"); 
    $("#hours").html(hours + "<span>Hours</span>"); 
    $("#minutes").html(minutes + "<span>Minutes</span>"); 
    $("#seconds").html(seconds + "<span>Seconds</span>"); 

    } 

    setInterval(function() { makeTimer(); }, 1000); 

    }); 

    </script> 
+0

檢查時'days','hours','minutes'和'seconds'是'0' ,然後停止間隔 –

+0

檢查我發給你的那一個。它應該工作。這個例子正在工作。 – grec0o

+0

已解決,感謝您的幫助! –

回答

0

只是檢查timeLeft小於或等於0

$(document).ready(function(){ 
    var timer = null; 

    function makeTimer() { 

     var endTime = new Date("September 5, 2016 19:50:50 UTC+02:00"), 
      endTime = (Date.parse(endTime))/1000, 
      now = new Date(), 
      now = (Date.parse(now)/1000), 
      timeLeft = endTime - now; 
     if(timeLeft > 0){ 
      var days = Math.floor(timeLeft/86400), 
       hours = Math.floor((timeLeft - (days * 86400))/3600), 
       minutes = Math.floor((timeLeft - (days * 86400) - (hours * 3600))/60), 
       seconds = Math.floor((timeLeft - (days * 86400) - (hours * 3600) - (minutes * 60))); 
      if (hours < "10") { hours = "0" + hours; } 
      if (minutes < "10") { minutes = "0" + minutes; } 
      if (seconds < "10") { seconds = "0" + seconds; }  

      $("#days").html(days + "<span>Days</span>"); 
      $("#hours").html(hours + "<span>Hours</span>"); 
      $("#minutes").html(minutes + "<span>Minutes</span>"); 
      $("#seconds").html(seconds + "<span>Seconds</span>"); 

     }else{ 
     clearInterval(timer); 
     $("#seconds").html("<span>00Seconds</span>"); 
     } 
} 
timer = setInterval(function() { makeTimer(); }, 1000); 

})

這裏是小提琴https://jsfiddle.net/69ax5j4L/

+0

沒有工作:C嘗試直接複製以及,我想我必須有一些語法錯誤.. –

1

放這樣的支票:

var days = (timeLeft > 0)? Math.floor(timeLeft/86400):0; 

保持了幾個小時,分鐘,相同條件下,秒

要停止計時:

if(days <> 0 && hours <>0 && minutes <>0 && seconds <>0) 
{ 
    setInterval(function() { makeTimer(); }, 1000); 

    }); 
} 
+0

沒有使它停止:C –

+0

以及它使天停止 –

+0

是的,你需要寫小時,分鐘和秒的相同條件。並在一切爲0時停止調用函數 – Ish

1
if (now <= endTime){ 
     var timeLeft = endTime - now; 
    } 

應該這樣做

0

我用這一個:

http://jsfiddle.net/fqPqS/349/

只需添加到您的代碼,此行:

var current = new Date(); 
if (current > endTime) { 
    clearInterval(interval); 
} 

,改變這一點:

var interval = setInterval(makeTimer, 1000);