-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
120 lines (99 loc) · 2.87 KB
/
index.html
File metadata and controls
120 lines (99 loc) · 2.87 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
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Stopwatch</title>
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css" rel="stylesheet">
<style>
.container {
margin-top: 50px;
}
#display {
font-size: 2em;
text-align: center;
margin-bottom: 20px;
color: #FFFFFF;
background-color: #3498db;
padding: 10px;
border-radius: 5px;
}
.btn-group {
text-align: center;
}
.btn-primary {
background-color: #2ecc71;
border-color: #2ecc71;
}
.btn-danger {
background-color: #e74c3c;
border-color: #e74c3c;
}
.btn-secondary {
background-color: #f39c12;
border-color: #f39c12;
}
</style>
</head>
<body>
<div class="container">
<div id="display">00:00:00</div>
<div class="btn-group">
<button id="startBtn" class="btn btn-primary" onclick="startTimer()">Start</button>
<button id="stopBtn" class="btn btn-danger" onclick="stopTimer()" disabled>Stop</button>
<button class="btn btn-secondary" onclick="reset()">Reset</button>
</div>
</div>
<!-- JavaScript code -->
<script>
let timer;
let hours = 0;
let minutes = 0;
let seconds = 0;
// Function to start the stopwatch
function startTimer() {
document.getElementById('startBtn').disabled = true;
document.getElementById('stopBtn').disabled = false;
// Start the timer
timer = setInterval(updateTime, 1000);
}
// Function to stop the stopwatch
function stopTimer() {
// Enable the start button and disable the stop button
document.getElementById('startBtn').disabled = false;
document.getElementById('stopBtn').disabled = true;
// Stop the timer
clearInterval(timer);
}
// Function to update the stopwatch time
function updateTime() {
seconds++;
// If seconds reach 60, reset seconds and increment minutes
if (seconds === 60) {
seconds = 0;
minutes++;
// If minutes reach 60, reset minutes and increment hours
if (minutes === 60) {
minutes = 0;
hours++;
}
}
document.getElementById('display').textContent = pad(hours) + ':' + pad(minutes) + ':' + pad(seconds);
}
// Function to reset the stopwatch
function reset() {
clearInterval(timer);
// Enable the start button and disable the stop button
document.getElementById('startBtn').disabled = false;
document.getElementById('stopBtn').disabled = true;
// Reset hours, minutes, seconds, and display
hours = minutes = seconds = 0;
document.getElementById('display').textContent = '00:00:00';
}
// Function to pad single-digit numbers with leading zeros
function pad(val) {
return val > 9 ? val : '0' + val;
}
</script>
</body>
</html>