My problem is that timer doesn’t pause correctly. When I hit pause it looks like it has stopped but actually it continues to cycle, and when I hit start it doesn’t continue from where I paused, but from the location it had reached.
<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;
});
});
You have two handlers for
$('.start').Try this: Live Demo
And remove this:
Update 1
If you need a stopwatch, not a lap timer, then here is the code: Live Demo