-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstopwatch.js
49 lines (42 loc) · 1.03 KB
/
stopwatch.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
let timer;
let minutes = 0;
let seconds = 0;
let milliseconds = 0;
let isRunning = false;
function startTimer() {
if (!isRunning) {
isRunning = true;
timer = setInterval(updateTime, 10);
}
}
function stopTimer() {
clearInterval(timer);
isRunning = false;
}
function resetTimer() {
clearInterval(timer);
minutes = 0;
seconds = 0;
milliseconds = 0;
isRunning = false;
updateTime();
}
function updateTime() {
milliseconds += 10;
if (milliseconds === 1000) {
milliseconds = 0;
seconds++;
if (seconds === 60) {
seconds = 0;
minutes++;
}
}
const display = document.querySelector('.display');
display.textContent = `${padNumber(minutes)}:${padNumber(seconds)}:${padNumber(milliseconds / 10)}`;
}
function padNumber(number) {
return number.toString().padStart(2, '0');
}
document.querySelector('.start').addEventListener('click', startTimer);
document.querySelector('.stop').addEventListener('click', stopTimer);
document.querySelector('.reset').addEventListener('click', resetTimer);