Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
18
rated 0 times [  19] [ 1]  / answers: 1 / hits: 13632  / 10 Years ago, sun, october 12, 2014, 12:00:00

I am implementing a stopwatch by using Javascript. I have basic html document setup and a javascript file called stopwatch.js in which I have the following code. I make use of setInterval function to execute the clockRunning function every 1 second(1000ms). This gives me control over sec, min and hour to increment them accordingly, but I am having difficulty with inserting millisecond into the stopwatch. How should I increment the millisecond from 0 to 1000 and then reset to zero?



I have tried by decreasing the interval time for setInterval function to be called every 1ms and then set millisecond variable to time%1000 in which time variable is increased by 1 every time the function is called. But it does not give the result I want. The millisecond seems to be increasing way too slow.





var running = 0
var time = 0;
var hour = 0;
var min = 0;
var sec = 0;
var millisec = 0;


function start(){
started = window.setInterval(clockRunning, 1000);
}

function stop(){
window.clearInterval(started);
}

function clockRunning(){
time++;
sec++;
if (sec == 60){
min += 1;
sec = 0;
if (min == 60){
hour += 1;
min = 0;
}


}

document.getElementById(display-area).innerHTML = (hour ? (hour > 9 ? hour : 0 + hour) : 00)
+ : + (min ? (min > 9 ? min : 0 + min) : 00) + : + (sec > 9 ? sec : 0
+ sec);
};

<!DOCTYPE html>
<html>
<head>
<meta charset=UTF-8>
<title>Stopwatch</title>
<script src=stopwatch.js></script>
<style>
#display-area { font-size: 20pt; }
</style>

</head>
<body>
<div>
<output id=display-area>00:00:00.000</output>
</div>
<div>
<button id=toggle-button onClick=start()>Start</button>
<button id=toggle-button onClick=stop()>Stop</button>
<button id=reset-button>Reset</button>
</div>
</body>
</html>




More From » stopwatch

 Answers
9

You should keep track of the starting time then subtract that time from the current time using a Date:





var timeBegan = null
, timeStopped = null
, stoppedDuration = 0
, started = null;

function start() {
if (timeBegan === null) {
timeBegan = new Date();
}

if (timeStopped !== null) {
stoppedDuration += (new Date() - timeStopped);
}
console.log(stoppedDuration);

started = setInterval(clockRunning, 10);
}

function stop() {
timeStopped = new Date();
clearInterval(started);
}

function reset() {
clearInterval(started);
stoppedDuration = 0;
timeBegan = null;
timeStopped = null;
document.getElementById(display-area).innerHTML = 00:00:00.000;
}

function clockRunning(){
var currentTime = new Date()
, timeElapsed = new Date(currentTime - timeBegan - stoppedDuration)
, hour = timeElapsed.getUTCHours()
, min = timeElapsed.getUTCMinutes()
, sec = timeElapsed.getUTCSeconds()
, ms = timeElapsed.getUTCMilliseconds();

document.getElementById(display-area).innerHTML =
(hour > 9 ? hour : 0 + hour) + : +
(min > 9 ? min : 0 + min) + : +
(sec > 9 ? sec : 0 + sec) + . +
(ms > 99 ? ms : ms > 9 ? 0 + ms : 00 + ms);
};

<!DOCTYPE html>
<html>
<head>
<meta charset=UTF-8>
<title>Stopwatch</title>
<style>
#display-area { font-size: 20pt; }
</style>

</head>
<body>
<div>
<output id=display-area>00:00:00.000</output>
</div>
<div>
<button id=toggle-button onClick=start()>Start</button>
<button id=toggle-button onClick=stop()>Stop</button>
<button id=reset-button onClick=reset()>Reset</button>
</div>
</body>
</html>





The reason you were seeing the milliseconds lagging before was that setInterval is notorious for not firing exactly when you specify. You can get around this using the strategy above.



Update: You could keep track of how long the timer has paused between resets. Updated my answer to accommodate this.


[#41938] Friday, October 10, 2014, 10 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
agustindejonm

Total Points: 738
Total Questions: 84
Total Answers: 84

Location: Northern Ireland
Member since Mon, Nov 14, 2022
2 Years ago
agustindejonm questions
Fri, Jun 25, 21, 00:00, 3 Years ago
Fri, Sep 18, 20, 00:00, 4 Years ago
Sat, May 16, 20, 00:00, 4 Years ago
;