如何减少js中的计时器值?

时间:2018-06-26 11:29:15

标签: javascript

我有一个代码用于递减计数等于6的计时器,但是我想像这样hrs:min:sec递减格式为00:00:10的count的值

function countdown() {
// starts countdown
if (count == 0) {
// time is up
} else {
count--;
t = setTimeout("countdown()", 1000);
}
};

,计数为00:00:10,持续10秒钟。如何减少计时器?有人可以帮我吗?

1 个答案:

答案 0 :(得分:0)

类似的事情将满足您的需求:

function showCountdown(countSeconds)
{  
   var countStatus = new Date(1000 * countSeconds).toISOString().substr(11, 8);
   document.getElementById('output').innerHTML = "Count: " + countStatus;
}
var count = 10;

function countdown() {
  // starts countdown
  if (count === 0) {
     return;
  }
  count--;
  setTimeout(countdown, 1000);
  showCountdown(count);
  
};

countdown();
<div id="output">
</div>

我已经将JSFiddle更新为HH:mm:ss格式 JSFiddle:https://jsfiddle.net/8uhq0vej/7/