-
Notifications
You must be signed in to change notification settings - Fork 13
/
Stopwatch.html
118 lines (110 loc) · 3.42 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
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
<!DOCTYPE html>
<html>
<head>
<title>Stopwatch</title>
<style>
body {
display: flex;
justify-content: center;
align-items: center;
min-height: 100vh;
margin: 0;
font-family: Arial, sans-serif;
background-color: #f0f0f0;
}
.container {
text-align: center;
background-color: white;
padding: 2rem;
border-radius: 10px;
box-shadow: 0 0 10px rgba(0,0,0,0.1);
}
.display {
font-size: 3.5rem;
font-weight: bold;
margin: 1rem 0;
color: #333;
}
.buttons {
display: flex;
gap: 1rem;
justify-content: center;
}
button {
padding: 0.8rem 1.5rem;
font-size: 1rem;
cursor: pointer;
border: none;
border-radius: 5px;
transition: transform 0.1s;
}
button:active {
transform: scale(0.95);
}
#startBtn {
background-color: #4CAF50;
color: white;
}
#stopBtn {
background-color: #f44336;
color: white;
}
#resetBtn {
background-color: #2196F3;
color: white;
}
</style>
</head>
<body>
<div class="container">
<h1>Stopwatch</h1>
<div class="display" id="display">00:00:00</div>
<div class="buttons">
<button id="startBtn">Start</button>
<button id="stopBtn">Stop</button>
<button id="resetBtn">Reset</button>
</div>
</div>
<script>
let startTime;
let elapsedTime = 0;
let timerInterval;
let isRunning = false;
const display = document.getElementById('display');
const startBtn = document.getElementById('startBtn');
const stopBtn = document.getElementById('stopBtn');
const resetBtn = document.getElementById('resetBtn');
function start() {
if (!isRunning) {
isRunning = true;
startTime = Date.now() - elapsedTime;
timerInterval = setInterval(updateDisplay, 10);
startBtn.textContent = 'Pause';
} else {
stop();
startBtn.textContent = 'Start';
}
}
function stop() {
isRunning = false;
clearInterval(timerInterval);
}
function reset() {
stop();
elapsedTime = 0;
startBtn.textContent = 'Start';
updateDisplay();
}
function updateDisplay() {
elapsedTime = Date.now() - startTime;
const milliseconds = Math.floor((elapsedTime % 1000) / 10);
const seconds = Math.floor((elapsedTime / 1000) % 60);
const minutes = Math.floor((elapsedTime / (1000 * 60)) % 60);
display.textContent = `${minutes.toString().padStart(2, '0')}:${seconds.toString().padStart(2, '0')}:${milliseconds.toString().padStart(2, '0')}`;
}
startBtn.addEventListener('click', start);
stopBtn.addEventListener('click', stop);
resetBtn.addEventListener('click', reset);
</script>
</body>
</html>