-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
64 lines (57 loc) · 1.52 KB
/
script.js
File metadata and controls
64 lines (57 loc) · 1.52 KB
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
"use strict";
let timer;
let isRunning = false;
let hundredthsOfSecond = 0;
let seconds = 0;
let minutes = 0;
let hours = 0;
function startChronometer() {
if (!isRunning) {
isRunning = true;
timer = setInterval(updateTime, 10);
document.getElementById("start").disabled = true;
document.getElementById("stop").disabled = false;
}
}
document.getElementById("stop").disabled = true;
function stopChronometer() {
if (isRunning) {
isRunning = false;
clearInterval(timer);
document.getElementById("start").disabled = false;
document.getElementById("stop").disabled = true;
}
}
function resetChronometer() {
stopChronometer();
hundredthsOfSecond = 0;
seconds = 0;
minutes = 0;
hours = 0;
updateDisplay();
}
function updateTime() {
hundredthsOfSecond++;
if (hundredthsOfSecond === 100) {
hundredthsOfSecond = 0;
seconds++;
if (seconds === 60) {
seconds = 0;
minutes++;
if (minutes === 60) {
minutes = 0;
hours++;
}
}
}
updateDisplay();
}
function updateDisplay() {
const timeString = `${hours}:${(minutes < 10 ? "0" : "") + minutes}:${
(seconds < 10 ? "0" : "") + seconds
}.${(hundredthsOfSecond < 10 ? "0" : "") + hundredthsOfSecond}`;
document.getElementById("display").textContent = timeString;
}
document.getElementById("start").addEventListener("click", startChronometer);
document.getElementById("stop").addEventListener("click", stopChronometer);
document.getElementById("reset").addEventListener("click", resetChronometer);