Jquery 个需要自行重置的计数器

Jquery counter that need to reset itself

您好,我在这里添加了代码:

jfiddle code here

我需要计时器在到达时间 0 时自行重新启动,以便它在 24 小时倒计时后再次启动。

你能帮我找到合适的代码吗?那个设置?

现在,当它到达时间 0 时,它以 -1、-2 等开始。

代码如下:

<div id="countdown"></div>

function ShowTime() {
  var now = new Date();
  var hrs = 18-now.getHours();
  var mins = 60-now.getMinutes();
  var secs = 60-now.getSeconds();
      timeLeft = "You now have "+hrs+' hours '+mins+' minuts '+secs+' seconds' +" To ensure delivery within 24 hours";
  $("#countdown").html(timeLeft);
}

var countdown;
function StopTime() {
    clearInterval(countdown);

}

setInterval(ShowTime ,1000);

谢谢

马丁

计数器只会在 1900 和 0000 之间变为负值 - 因此为 hrs 变量创建一个 if 语句就足够了。在此声明中,您决定现在从 18(或您的截止日期是什么)或从 18 + 24 中减去:

function ShowTime() {
  var now = new Date();
  if (now.getHours() > 18){
    var hrs =18-now.getHours() +24;
  }else{
    var hrs = 18-now.getHours();
  }

 var mins = 60-now.getMinutes();
 var secs = 60-now.getSeconds();

  timeLeft = "You now have "+hrs+' hours '+mins+' minuts '+secs+' seconds' +" To ensure delivery within 24 hours";
  $("#countdown").html(timeLeft);
}

更新了您的 fiddle:http://jsfiddle.net/srwpyac0/4/