我的問題是計時器不能正常暫停。當我暫停時,它看起來已停止,但實際上它繼續循環,當我點擊開始時,它不會從我暫停的地方繼續,而是從它已到達的位置繼續。計時器不工作
<div class="stopwatch">
<span>00:00:00,000</span><br />
<div class="btn start">play</div>
<div class="btn pause">pause</div>
<div class="btn reset">reset</div>
</div>
$(function(){
var reload = 1000/60;
var timer = null;
var startTime = 0;
var btn = $('.stopwatch a');
var count = $('.stopwatch span');
var pause = false;
$('.pause').click(function(){
pause = true;
});
$('.start').click(function(){
pause = false;
});
$('.reset').click(function(){
return ((count.text('00:00:00,000')) && (timer = 0));
});
function zero(num, length) {
if (typeof(length) == 'undefined') length = 2;
while (num.toString().length < length) {
num = '0' + num;
}
return num;
}
function zero_format(time){
return zero(time.getUTCHours()) + ':' + zero(time.getMinutes()) + ':' + zero(time.getSeconds()) + ',' + zero(time.getMilliseconds());
}
$('.start').click(function(){
if (!timer){
startTime = new Date();
timer = setInterval(function(){
if (pause){
return;
}
var currentTime = new Date(new Date() - startTime);
count.text(zero_format(currentTime));
}, reload);
}
return false;
});
});
代碼對我很好。你能否讓你的問題更清楚? – Ergec
任何登錄螢火蟲? –
暫停 - >開始後,我發現它工作不正常。它看起來像計時器實際上並沒有停止 –