-
Notifications
You must be signed in to change notification settings - Fork 0
/
Stopwatch.html
128 lines (102 loc) · 3.48 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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
* {
margin-top: 10px;
}
body {
text-align: center;
}
div span {
margin: 15px;
border: 1px solid black;
padding: 10px;
border-radius: 5px;
}
div button {
margin: 14px;
cursor: pointer;
padding: 5px;
border: 1px solid black;
color: white;
background: rgba(79, 151, 245, 0.808);
border-radius: 5px;
outline: none;
}
div button:hover {
background: rgba(21, 88, 175, 0.808);
}
</style>
</head>
<body>
<div><span id="hour">00</span>:<span id="min">00</span>:<span id="sec">00</span></div>
<div><button id="start">start</button> <button id="stop">stop</button> <button id="reset">reset</button></div>
<div><span id="hour_time">00</span>:<span id="min_time">00</span>:<span id="sec_time">00</span></div>
<script>
let hour = document.getElementById('hour');
let min = document.getElementById('min');
let sec = document.getElementById('sec');
const start = document.getElementById('start');
const stopp = document.getElementById('stop');
const reset = document.getElementById('reset');
let hourVal = 0;
let minVal = 0;
let secVal = 0;
const timer = () => {
secVal += 1;
sec.innerText = `0${secVal}`
if (secVal >= 10) {
sec.innerText = secVal;
}
if (secVal == 60) {
minVal += 1;
min.innerText = `0${minVal}`;
if (minVal >= 10) {
min.innerText = minVal;
}
secVal = 0;
sec.innerText = `0${secVal}`
}
if (minVal == 60) {
hourVal += 1;
hour.innerText = `0${hourVal}`;
if (hourVal >= 10) {
hour.innerText = hourVal;
}
minVal = 0;
min.innerText = `0${minVal}`;
}
}
start.addEventListener('click', () => {
let t = setInterval(timer, 100)
stopp.addEventListener('click', () => {
clearInterval(t)
})
reset.addEventListener('click', () => {
clearInterval(t);
hourVal = 0;
hour.innerText = `0${hourVal}`
minVal = 0;
min.innerText = `0${minVal}`
secVal = 0;
sec.innerText = `0${secVal}`
})
})
let sec_time = document.getElementById('sec_time');
let min_time = document.getElementById('min_time');
let hour_time = document.getElementById('hour_time');
let date = new Date()
hour_time.innerText = date.getHours();
min_time.innerText = date.getMinutes();
sec_time.innerText = date.getSeconds();
setInterval(() => {
sec_time.innerText = date.getSeconds();
}, 100)
</script>
</body>
</html>