为什么我的倒计时代码变为负数

时间:2016-01-21 12:25:45

标签: javascript arrays variables

function ShowTime() {
  var now = new Date();
  var hrs = 19-now.getHours();
  var mins = 60-now.getMinutes();
  var secs = 60-now.getSeconds();
      timeLeft = "  " +hrs+'      '+mins+'      '+secs+' ';
  $("#countdown").html(timeLeft);
}

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

}

setInterval(ShowTime ,1000);

晚上8:30在这里。我希望如果他到了晚上9点,倒计时再次重启,它会在每天晚上9点重新启动..而且它会变为负值..

3 个答案:

答案 0 :(得分:0)

如果您想使用countdown

,则应指定clearInterval
countdown = setInterval(ShowTime ,1000);

答案 1 :(得分:0)

请制作这样的逻辑。

If now >19 then
timeLeft is (24-now)+19
else hrs = 19-now;

答案 2 :(得分:0)

function ShowTime() {
  var now = new Date();
var hrs;
var currentHours = now.getHours();
if(currentHours>19){
hrs = 24-currentHours+19;
}else{
  hrs = 19-now.getHours();
}
  var mins = 60-now.getMinutes();
  var secs = 60-now.getSeconds();
      timeLeft = "  " +hrs+'      '+mins+'      '+secs+' ';
  $("#countdown").html(timeLeft);
}
相关问题