计时器将在加载页面时从10分钟开始倒计时。如果我在一段时间后做了mousedown,倒计时应该重新开始。 但是,计时器将在两个值之间跳转。似乎两者都在计算。如何删除现有的间隔?
Template.content.onRendered(function() {
var display = document.querySelector('#timer');
startTimer( 600, display);
});
Template.content.events({
'mousedown': function() {
var display = document.querySelector('#timer');
startTimer( 600, display);
// this will start a new counter
// but I want the old one to be replaced
}
});
startTimer = function(duration, display) {
var start = Date.now(),
diff,
minutes,
seconds;
function timer() {
diff = duration - (((Date.now() - start) / 1000) | 0);
minutes = (diff / 60) | 0;
seconds = (diff % 60) | 0;
minutes = minutes < 10 ? "0" + minutes : minutes;
seconds = seconds < 10 ? "0" + seconds : seconds;
display.textContent = minutes + ":" + seconds;
}
timer();
setInterval(timer, 1000);
};
答案 0 :(得分:1)
在js中添加一个新变量
var timer = 0;
从{/ 1>}函数更新您的代码
startTimer
到
setInterval(timer, 1000);
然后在timer = setInterval(timer, 1000);
事件处理函数中添加以下行
mousedown