-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathStopwatch
87 lines (87 loc) · 2.28 KB
/
Stopwatch
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
HTML:
<!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 rel="stylesheet" href="styles.css">
</head>
<body>
<div class="stopwatch-container">
<h1>Stopwatch</h1>
<p id="stopwatchDisplay">00:00:00</p>
<div class="buttons">
<button id="startButton">Start</button>
<button id="stopButton">Stop</button>
<button id="resetButton">Reset</button>
</div>
</div>
<script src="script.js"></script>
</body>
</html>
CSS:
body {
font-family: Arial, sans-serif;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
margin: 0;
}
.stopwatch-container {
text-align: center;
background-color: #f0f0f0;
padding: 20px;
border-radius: 10px;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
}
#stopwatchDisplay {
font-size: 36px;
margin-bottom: 20px;
}
.buttons {
display: flex;
justify-content: center;
gap: 10px;
}
button {
padding: 10px 20px;
font-size: 16px;
cursor: pointer;
}
JavaScript:
let timer;
let startTime;
let elapsedTime = 0;
let isRunning = false;
function updateDisplay() {
const display = document.getElementById('stopwatchDisplay');
const time = new Date(elapsedTime);
const minutes = time.getUTCMinutes().toString().padStart(2, '0');
const seconds = time.getUTCSeconds().toString().padStart(2, '0');
const milliseconds = Math.floor(time.getUTCMilliseconds() / 10).toString().padStart(2, '0');
display.textContent = `${minutes}:${seconds}:${milliseconds}`;
}
document.getElementById('startButton').addEventListener('click', function() {
if (!isRunning) {
startTime = Date.now() - elapsedTime;
timer = setInterval(function() {
elapsedTime = Date.now() - startTime;
updateDisplay();
}, 10);
isRunning = true;
}
});
document.getElementById('stopButton').addEventListener('click', function() {
if (isRunning) {
clearInterval(timer);
isRunning = false;
}
});
document.getElementById('resetButton').addEventListener('click', function() {
clearInterval(timer);
elapsedTime = 0;
updateDisplay();
isRunning = false;
});