-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathclock.html
More file actions
49 lines (40 loc) · 1.28 KB
/
clock.html
File metadata and controls
49 lines (40 loc) · 1.28 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
<!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>
</head>
<body>
<div id="clock">
<span class="hour">hh</span>:<span class="min">mm</span>:<span class="sec">ss</span>
</div>
<button onclick="clockStart()">Start</button>
<button onclick="clockStop()">Stop</button>
<script>
function update() {
let clock = document.getElementById('clock')
let date = new Date()
let hours = date.getHours()
if (hours < 10) hours = '0' + hours
clock.children[0].innerHTML = hours
let minutes = date.getMinutes()
if (minutes < 10) minutes = '0' + minutes
clock.children[1].innerHTML = minutes
let seconds = date.getSeconds()
if (seconds < 10) seconds = '0' + seconds
clock.children[2].innerHTML = seconds
}
let timerId
function clockStart() {
timeId = setInterval(update, 1000)
update()
}
function clockStop() {
clearInterval(timerId)
timerId = null
}
</script>
</body>
</html>