-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstopwatch(css+js).html
More file actions
139 lines (123 loc) · 3.76 KB
/
stopwatch(css+js).html
File metadata and controls
139 lines (123 loc) · 3.76 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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Stopwatch</title>
<style>
* {
box-sizing: border-box;
margin: 0;
padding: 0;
}
body {
font-family: Arial, sans-serif;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
background-color: #f4f4f4;
}
.stopwatch {
background-color: #fff;
padding: 30px;
border-radius: 10px;
box-shadow: 0 4px 10px rgba(0, 0, 0, 0.1);
text-align: center;
}
.time-display {
font-size: 3em;
margin-bottom: 20px;
font-weight: bold;
}
.buttons {
display: flex;
gap: 20px;
justify-content: center;
}
.button {
font-size: 1.5em;
padding: 15px 30px;
border: none;
border-radius: 5px;
background-color: #4caf50;
color: white;
cursor: pointer;
transition: background-color 0.3s;
}
.button:hover {
background-color: #45a049;
}
.button:disabled {
background-color: #ddd;
cursor: not-allowed;
}
.reset-button {
background-color: #f44336;
}
.reset-button:hover {
background-color: #e53935;
}
</style>
</head>
<body>
<div class="stopwatch">
<div id="time-display" class="time-display">00:00:00.00</div>
<div class="buttons">
<button id="start-stop" class="button" onclick="startStop()">Start</button>
<button id="reset" class="button reset-button" onclick="reset()">Reset</button>
</div>
</div>
<script>
let timer;
let isRunning = false;
let seconds = 0;
let minutes = 0;
let hours = 0;
let milliseconds = 0;
function formatTime() {
const pad = (num) => num < 10 ? '0' + num : num;
const padMilliseconds = (num) => num < 10 ? '0' + num : num;
return `${pad(hours)}:${pad(minutes)}:${pad(seconds)}.${padMilliseconds(milliseconds)}`;
}
function updateTimeDisplay() {
document.getElementById("time-display").innerText = formatTime();
}
function startStop() {
if (isRunning) {
clearInterval(timer);
document.getElementById("start-stop").innerText = "Start";
} else {
timer = setInterval(() => {
milliseconds++;
if (milliseconds === 100) {
milliseconds = 0;
seconds++;
if (seconds === 60) {
seconds = 0;
minutes++;
if (minutes === 60) {
minutes = 0;
hours++;
}
}
}
updateTimeDisplay();
}, 10); // Update every 10 milliseconds
document.getElementById("start-stop").innerText = "Stop";
}
isRunning = !isRunning;
}
function reset() {
clearInterval(timer);
isRunning = false;
milliseconds = 0;
seconds = 0;
minutes = 0;
hours = 0;
updateTimeDisplay();
document.getElementById("start-stop").innerText = "Start";
}
</script>
</body>
</html>