forked from Pranavv213/WebDevForBeginners
-
Notifications
You must be signed in to change notification settings - Fork 0
/
StopWatch.html
153 lines (143 loc) · 3.3 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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<meta charset="UTF-8">
<title> Stopwatch Timer</title>
<style>
/* Google fonts import link */
@import url('https://fonts.googleapis.com/css2?family=Poppins:wght@200;300;400;500;600;700&display=swap');
*{
margin: 0;
padding: 0;
box-sizing: border-box;
font-family: 'Poppins',sans-serif;
}
body{
height: 100vh;
display: flex;
align-items: center;
justify-content: center;
background: #7D2Ae8;
}
.wrapper{
user-select: none;
}
.wrapper .time{
height: 100px;
background: #fff;
display: flex;
align-items: center;
justify-content: center;
border-radius: 6px;
box-shadow: 10px 10px 20px rgba(0,0,0,0.09);
padding: 0 10px;
}
.wrapper .time span{
width: 100px;
text-align: center;
font-size: 50px;
font-weight: 500;
color: #333;
}
.time span.colon{
width: 10px;
}
.time span.ms-colon,
.time span.millisecond{
color: #7D2Ae8;
}
.wrapper .buttons{
text-align: center;
margin-top: 20px;
}
.buttons button{
padding: 6px 16px;
outline: none;
border: none;
margin: 0 5px;
color: #7D2Ae8;
background: #fff;
font-size: 19px;
font-weight: 500;
border-radius: 4px;
cursor: pointer;
box-shadow: 10px 10px 20px rgba(0,0,0,0.09);
}
.buttons button.active,
.buttons button.stopActive{
pointer-events: none;
opacity: 0.7;
}
</style>
</head>
<body>
<div class="wrapper">
<div class="time">
<span class="hour">00</span>
<span class="colon">:</span>
<span class="minute">00</span>
<span class="colon">:</span>
<span class="second">00</span>
<span class="colon ms-colon">:</span>
<span class="millisecond">00</span>
</div>
<div class="buttons">
<button class="start">Start</button>
<button class="stop">Stop</button>
<button class="reset">Reset</button>
</div>
</div>
<script>
let hr = min = sec = ms = "0" + 0,
startTimer;
const startBtn = document.querySelector(".start"),
stopBtn = document.querySelector(".stop"),
resetBtn = document.querySelector(".reset");
startBtn.addEventListener("click", start);
stopBtn.addEventListener("click", stop);
resetBtn.addEventListener("click", reset);
function start() {
startBtn.classList.add("active");
stopBtn.classList.remove("stopActive");
startTimer = setInterval(()=>{
ms++
ms = ms < 10 ? "0" + ms : ms;
if(ms == 100){
sec++;
sec = sec < 10 ? "0" + sec : sec;
ms = "0" + 0;
}
if(sec == 60){
min++;
min = min < 10 ? "0" + min : min;
sec = "0" + 0;
}
if(min == 60){
hr++;
hr = hr < 10 ? "0" + hr : hr;
min = "0" + 0;
}
putValue();
},10);
}
function stop() {
startBtn.classList.remove("active");
stopBtn.classList.add("stopActive");
clearInterval(startTimer);
}
function reset() {
startBtn.classList.remove("active");
stopBtn.classList.remove("stopActive");
clearInterval(startTimer);
hr = min = sec = ms = "0" + 0;
putValue();
}
function putValue() {
document.querySelector(".millisecond").innerText = ms;
document.querySelector(".second").innerText = sec;
document.querySelector(".minute").innerText = min;
document.querySelector(".hour").innerText = hr;
}
</script>
</body>
</html>