-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathstopWatch.html
89 lines (74 loc) · 2.21 KB
/
stopWatch.html
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Stopwatch</title>
</head>
<body>
<!-- html code goes here -->
<h1 id="timer"></h1>
<button id="start">Start</button>
<button id="stop">Stop</button>
<button id="pause">Pause</button>
<script>
// javascript code goes here
let hours = 0;
let minutes = 0;
let seconds = 0;
let timerInterval;
const timerElement = document.querySelector("#timer");
const startButton = document.querySelector("#start");
const stopButton = document.querySelector("#stop");
const pauseButton = document.querySelector("#pause");
stopButton.disabled = true;
pauseButton.disabled = true;
timerElement.innerHTML = "00:00:00";
function formatTime(time) {
return time < 10 ? "0" + time : time;
}
function updateTimer() {
seconds++;
if (seconds === 60) {
seconds = 0;
minutes++;
if (minutes === 60) {
minutes = 0;
hours++;
}
}
timerElement.textContent =
formatTime(hours) +
":" +
formatTime(minutes) +
":" +
formatTime(seconds);
}
startButton.addEventListener("click", () => {
startButton.disabled = true;
stopButton.disabled = false;
pauseButton.disabled = false;
timerInterval = setInterval(updateTimer, 1000);
});
stopButton.addEventListener("click", () => {
startButton.disabled = false;
stopButton.disabled = true;
pauseButton.disabled = true;
clearInterval(timerInterval);
hours = 0;
minutes = 0;
seconds = 0;
timerElement.textContent = "00:00:00";
});
pauseButton.addEventListener("click", () => {
if (pauseButton.textContent === "Pause") {
clearInterval(timerInterval);
pauseButton.textContent = "Continue";
} else {
timerInterval = setInterval(updateTimer, 1000);
pauseButton.textContent = "Pause";
}
});
</script>
</body>
</html>