I know this question has been asked before and I did check relevant previous posts, but no joy so far with my code. I can get my timer to pause, however, when I resume, it seems to have ignored the pause function and the time is displayed as if the timer had not been paused.
<!DOCTYPE html>
<html>
<head>
<script>
var dt = null;
var val;
function startTimer(){
setTimeout(setTime, 100);
}
function setTime() {
if (dt == null)
dt = new Date();
var totalseconds = parseInt(((new Date()) - dt) / 1000, 10);
var hh = parseInt(totalseconds / (60 * 60), 10);
var mm = parseInt((totalseconds - (hh * 60 * 60)) / 60, 10);
var ss = parseInt(totalseconds - (hh * 60 * 60) - (mm * 60), 10);
document.getElementsByName("txttimer")[0].value = (hh > 9 ? hh : ('0' + hh))
+ ":" + (mm > 9 ? mm : ('0' + mm)) + ":" + (ss > 9 ? ss : ('0' + ss));
val = setTimeout(setTime,1000);
}
function pauseTimer() {
clearTimeout(val);
}
function resumeTimer() {
val = setTimeout(setTime,1000);
}
</script>
</head>
<body>
<form >
<input onclick="startTimer()" type="button" value="START" style="position: absolute;
margin-left: 20%; margin-top: 10%; width: 75px;"/>
<input onclick="pauseTimer()" type="button" value="PAUSE" style="position: absolute;
margin-left: 20%; margin-top: 15%; width: 75px;"/>
<input onclick="resumeTimer()" type="button" value="RESUME" style="position: absolute;
margin-left: 20%; margin-top: 20%; width: 75px;"/>
<input type="text" readonly name="txttimer" style="position: absolute; width: 100px;
margin-left: 30%; margin-top: 10%; text-align: center"/>
</form>
</body>
</html>
I would be really grateful if someone could help me out.
totalSeconds(which are displayed): From the current time and fromdt… What do you think could need to be altered?parseInt()is meant for strings so I don't understand what your comment means.parseInt()on things that are already numbers to start with (rather thanMath.floor). By usingparseInt()you force javascript to first convert a number to a string just so it can convert it back to a number.