when I click start, the count begins, and the start button hides and shows the pause and reset buttons. If I click start and reset repeatedly, the count will no longer count correctly and the reset buttons and pause buttons won’t even matter anymore.
var c = 0;
var t;
function count(){
document.getElementById("count").innerHTML = c;
c = c + 1;
t = setTimeout(count, 1000);
}
function start(){
count();
document.getElementById("startb").style.display = "none";
document.getElementById("pauseb").style.display = "inline";
}
function pause(){
clearTimeout(t);
document.getElementById("startb").style.display = "inline";
document.getElementById("pauseb").style.display = "none";
document.getElementById("startb").innerHTML = "Resume";
document.getElementById("resetb").style.display = "inline";
}
function reset(){
c = 0;
clearTimeout(t);
document.getElementById("count").innerHTML = c;
document.getElementById("startb").style.display = "inline";
document.getElementById("startb").innerHTML = "Start";
document.getElementById("pauseb").style.display = "none";
document.getElementById("resetb").style.display = "none";
}
*{
margin: 0;
padding: 0;
font-family: Arial;
}
body{
background-color: #333;
color: black;
}
button{
width: 100px;
height: 30px;
line-height: 30px;
text-align: center;
color: #000;
background-color: #c3c3c3;
border: 1px solid #000;
}
#count{
font-size: 175px;
}
#startb {
background-color: #800080;
border-color: #220022;
color: #2e002e;
}
#pauseb {
display: none;
background-color: black;
color: #b6b6b6;
}
#resetb {
display: none;
background-color:red;
color: #3b0000;
border-color: #3b0000;
font-weight: bold;
}
#lapb{
display: none;
background-color: #008800;
color: black;
border-color: darkgreen;
font-weight: bold;
}
<!DOCTYPE html>
<html>
<head>
<title>Stopwatch</title>
<script src="Stopwatch.js"></script>
<link rel="stylesheet" href="Stopwatch.css">
</head>
<body>
<h1>Stopwatch</h1>
<span id="count">0</span><br />
<button id="startb" onClick="start()">Start</button>
<button id="pauseb" onClick="pause()">Pause</button>
<button id="resetb" onClick="reset()">Reset</button>
<button id="lapb" onClick="lap()">Lap</button>
</body>
</html>
i havent tried anything im guessing multiple instances of the start function are being processed, im thinking if else statements somewhere could prevent this.
2
Answers
Try this. It has precision upto milliseconds
Edit:
Request animation frame is not the right solution for this use case for the following reasons:
requestAnimationFrame
calls are paused when running in background tabs or hidden iframe tags which is not ideal for a stopwatch appRead more about
requestAnimationFrame
on MDNsetInterval
(specially not set to such an expensive ms interval) when in need to do frequent updates to your UI. You don’t want to cog the main thread with setInterval, instead use the absolute winner in performance, and that’s the Window.requestAnimationFrame.Element.style
from JS, instead use the Element.classList methods like.add()
,.remove()
Here’s a better and performant example for a stopwatch that previews
h+:m:s.mil
and will work for any number of stopwatch on a single page.