-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStopWatch Program JAVASCRIPT.txt
More file actions
140 lines (118 loc) · 3.11 KB
/
StopWatch Program JAVASCRIPT.txt
File metadata and controls
140 lines (118 loc) · 3.11 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
140
const display = document.getElementById("display");
let timer = null;
let startTime = 0;
let elapsedTime = 0;
let isRunning = false;
function start() {
if (!isRunning) {
startTime = Date.now() - elapsedTime;
timer = setInterval(update, 10);
isRunning = true;
}
}
function stop() {
if (isRunning) {
clearInterval(timer);
elapsedTime = Date.now() - startTime;
isRunning = false;
}
}
function reset() {
clearInterval(timer);
startTime = 0;
elapsedTime = 0;
isRunning = false;
display.textContent = "00:00:00:00";
}
function update() {
const currentTime = Date.now();
elapsedTime = currentTime - startTime;
let hours = Math.floor(elapsedTime / (1000 * 60 * 60));
let minutes = Math.floor((elapsedTime / (1000 * 60)) % 60);
let seconds = Math.floor((elapsedTime / 1000) % 60);
let milliseconds = Math.floor((elapsedTime % 1000) / 10);
hours = String(hours).padStart(2, "0");
minutes = String(minutes).padStart(2, "0");
seconds = String(seconds).padStart(2, "0");
milliseconds = String(milliseconds).padStart(2, "0");
display.textContent = `${hours}:${minutes}:${seconds}:${milliseconds}`;
}
<!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="style.css">
</head>
<body>
<h1 id="myH1">Stopwatch</h1>
<div id="container">
<div id="display">
00:00:00:00
</div>
<div id="controls">
<button id="startBtn" onclick="start()">Start</button>
<button id="stopBtn" onclick="stop()">Stop</button>
<button id="resetBtn" onclick="reset()">Reset</button>
</div>
</div>
<script src="index.js"></script>
</body>
</html>
body {
display: flex;
flex-direction: column;
align-items: center;
background-color: hsl(0, 0%, 90%);
}
#myH1 {
font-size: 4rem;
font-family: "Arial", sans-serif;
color: hsl(0, 0%, 25%);
}
#container {
display: flex;
flex-direction: column;
align-items: center;
padding: 30px;
border: 5px solid;
background-color: white;
}
#display {
font-size: 5rem;
font-family: monospace;
font-weight: bold;
color: hsl(0, 0%, 30%);
text-shadow: 2px 2px 2px hsla(0, 0%, 0%, 0.75);
}
#controls button {
font-size: 1.5rem;
font-weight: bold;
padding: 10px 20px;
margin: 5px;
min-width: 125px;
border: none;
border-radius: 10px;
cursor: pointer;
color: white;
transition: background-color 0.5s ease;
}
#startBtn {
background-color: hsl(115, 100%, 40%);
}
#startBtn:hover {
background-color: hsl(115, 100%, 30%);
}
#stopBtn {
background-color: hsl(10, 90%, 50%);
}
#stopBtn:hover {
background-color: hsl(10, 90%, 40%);
}
#resetBtn {
background-color: hsl(205, 90%, 60%);
}
#resetBtn:hover {
background-color: hsl(205, 90%, 50%);
}