Jquery倒计时

时间:2016-09-05 08:50:18

标签: jquery

我正在进行“倒计时”但是当它达到0时,它会计入负数; -1 -2等等。我想让我停在0点。

我有:“天,小时,分钟和秒”

当一切都是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>

4 个答案:

答案 0 :(得分:1)

像这样检查:

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

保持小时,分钟,秒的相同条件

停止计时器:

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

    });
}

答案 1 :(得分:1)

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

应该这样做

答案 2 :(得分: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/

答案 3 :(得分:0)

我正在使用这个:

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

只需在代码中添加以下内容:

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

并且还改变了这个:

 var interval = setInterval(makeTimer, 1000);