-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclock.html
44 lines (39 loc) · 1.36 KB
/
clock.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
<html>
<body>
<div id="clock"> 8:10:45 </div>
<script>
class Clock {
constructor(){
this.time = new Date();
this.hour = this.time.getHours();
this.min = this.time.getMinutes();
this.sec = this.time.getSeconds();
this.am_pm = " AM ";
}
showTime(){
if (this.hour > 12) {
this.hour -= 12;
this.am_pm = " PM ";
}
if (this.hour == 0) {
this.hour = 12;
this.am_pm = " AM ";
}
this.updateTime();
currentTime = this.hour + " : " + this.min + " : " + this.sec + this.am_pm;
document.getElementById("clock").innerHTML = currentTime;
}
updateTime(){
this.hour = this.hour < 10 ? " 0 " + this.hour : this.hour;
this.min = this.min < 10 ? " 0 " + this.min : this.min;
this.sec = this.sec < 10 ? " 0 " + this.sec : this.sec;
}
}
myclock = new Clock();
function runClock(){
myclock.showTime();
}
setInterval(runClock, 1000);
</script>
</body>
</html>