【问题标题】:Javascript Countdown Timer Counting Down Not Updating Seconds/Minutes ProperlyJavascript倒数计时器倒数未正确更新秒/分钟
【发布时间】:2015-04-07 02:20:29
【问题描述】:

我创建了一个倒数计时器。但是,它不能正确倒计时。它不是每秒或每分钟倒计时,而是按百次倒计时并且不能正确更新。我使用相同的代码创建了另一个计数计时器,但它可以正常地按增量计数 - 每经过 60 秒就会计数分钟。我想知道如何使这个计时器正确倒计时。

JSBin: http://jsbin.com/funiveremi/6/edit

更新* 添加 60 秒计数器: http://jsbin.com/hohusuvube/2/edit

HTML

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>User Input Timer</title>
  <script type="text/javascript" src="part2.js"></script>
</head>
<body>

<h1><div id="time">00:00:00</div></h1>
<div id="result"></div>
<button id="start" onclick ="startClock();" >Start</button>
<button id="stop" onclick="stopTimer();">Stop</button>
<button id="clear" onclick="resetTimer();">Reset</button>

</body>
</html>

JAVASCIPT

hundreths = 10;
seconds = 15;
minutes = 20; 


document.getElementById("time").innerHTML = minutes + ":" + seconds + ":" + hundreths;


var currentTime = document.getElementById('time');

var t;

function startClock() {
function add() {

    hundreths--;
    if (hundreths < 0) {
        hundreths = 0;
        seconds--;
        if (seconds < 0) {
            seconds = 0;
            minutes--;
        }
        if(minutes < 0) {
            seconds= 0;
            minutes= 0;
            stopTimer();

        }
    }



if (hundreths > 9 && seconds < 9) {
currentTime.innerHTML = "0" + minutes + ":" + "0" + seconds + ":" + hundreths;
  }
else if ((seconds > 9 ) && (hundreths < 9)) {
currentTime.innerHTML = "0" + minutes + ":" + seconds + ":" + "0" + hundreths;
}
else if((seconds > 9) && (hundreths > 9)) {
currentTime.innerHTML = "0" + minutes + ":" + seconds + ":" + hundreths;
}
else if ((minutes > 9) && (seconds < 9) && (hundreths < 9)) {
currentTime.innerHTML = minutes + ":" + "0" + seconds + ":" + "0" + hundreths;
}

else if ((minutes > 9) && (seconds > 9) && (hundreths < 9)) {
currentTime.innerHTML = minutes + ":" + seconds + ":" + "0" + hundreths;
}

else if ((minutes > 9) && (seconds > 9) && (hundreths < 9)) {
currentTime.innerHTML = minutes + ":" + seconds + ":" + hundreths;
}

else {
currentTime.innerHTML = "0" + minutes + ":" + "0" + seconds + ":" + "0" + hundreths;
}

   timer();
} // end function add();


function timer() {
    t = setTimeout(add, 100);
}
timer();
} // end function startClock();

function stopTimer() {
    document.getElementById("result").innerHTML = "<p>" + ("Your time is: " + minutes + " minutes, " + seconds + " seconds, " + "and " + hundreths + " hundreths") + "</p>";
    clearTimeout(t);
} // end function stopTimer();


function resetTimer() {
clearTimeout(t);

  currentTime.innerHTML = "00:00:00";
} // end function resetTimer();

【问题讨论】:

    标签: javascript countdowntimer


    【解决方案1】:

    只需使用 Jquery Countdown 插件即可。

    http://hilios.github.io/jQuery.countdown/

    实施会更容易。像这样的。

    <div id="getting-started"></div>
    <script type="text/javascript">
      $("#getting-started").countdown("2016/01/01", function(event) {
        $(this).text(event.strftime('%D days %H:%M:%S'));
      });
    </script>
    

    【讨论】:

      【解决方案2】:

      这会起作用,你有关于何时设置不正确的秒数的逻辑,所以它会不断地自我重置。在这种情况下,如果您遇到困难,最好的办法之一就是尝试更改 if 语句,删除其中一些语句并使用不同的值。它可以帮助您了解系统中真正发生的事情,并为您提供有关问题所在的线索。另一个技巧是用普通的英语大声朗读代码。当您大声阅读它时,您通常会通过听到自己说出逻辑来意识到自己是错误的。

          hundreths--;
          if (hundreths < 0) {
              hundreths = 10;
              seconds--;
              if (seconds < 0) {
                  seconds = 59;
                  minutes--;
              }
              if(minutes < 0) {
                  seconds= 0;
                  minutes= 0;
                  stopTimer();
      
              }
          }
      

      【讨论】:

      • 谢谢!!这些都是非常有用的提示
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-04-19
      相关资源
      最近更新 更多