Jquery计数器需要重置自己

时间:2015-01-15 09:26:08

标签: jquery countdowntimer

你好我在这里添加了代码:

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);

谢谢

马丁

1 个答案:

答案 0 :(得分:0)

计数器只在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);
}

更新了你的小提琴:http://jsfiddle.net/srwpyac0/4/