Sunday, 15 September 2013

Can't get a pause function to work with my javascript stopwatch

Can't get a pause function to work with my javascript stopwatch

Can some one help me. I can't seem to get this stopwatch to pause and
displayed the paused(stopped) time, and then reactivate when I hit start
again.
I don't know how to stop the timer from counting up. Not sure if it's best
practice to end the timer function, create a new function for current
time, or to keep subtracting 1 from it using the setInterval?
<script type="text/javascript">
var digit=-1.0;
var min=0;
var time;
function timer(){
digit++;
if(digit>59){
min++;
document.getElementById("mins").innerHTML=padTimer(min);
digit=0;
}
document.getElementById("secs").innerHTML=padTimer(digit);
}
function padTimer(x) {
if (x<=9) { x = ("0"+x); }
return x;
}
function start(){
time=setInterval(timer, 1000);
timer();
}
function pause() {
}
function reset(){
digit=-1.0;
timerPay=0;
}
</script>
<a href="#" onclick="start()">Click here to start the timer</a>
<a href="#" onclick="pause()">Click here to pause the timer</a>
<a href="#" onclick="reset()">Click here to reset the timer</a>
<div>
<span id="mins" >00</span>:<span id="secs">00</span><br>
</div>

No comments:

Post a Comment