DOCTYPE HTML html body style hours color red minutes color green secon

 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
<!DOCTYPE HTML>
<html>
<body>
<style>
.hours{
color:red;
}
.minutes{
color:green;
}
.seconds{
color:blue;
}
</style>
<div id="clock">
<span class="hours">00:</span>
<span class="minutes">00:</span>
<span class="seconds">00</span>
<input class="start" type="button" onclick="clockStart()" value="Start">
<input class="stop" type="button" onclick="clockStop()" value="Stop">
</div>
<script>
var ss = 0;
var mm = 0;
var hh = 0;
var timerID;
function clockOn(){
ss++;
var clock = document.getElementById("clock");
if (ss == 60){
ss = 0;
mm++;
}
if (hh < 10)
h0 = "0";
else
h0 = null;
if (mm < 10)
m0 = "0";
else
m0 = null;
if (ss < 10)
s0 = "0";
else
s0 = null;
clock.children[0].innerHTML = h0 + hh + ":";
clock.children[1].innerHTML = m0 + mm + ":";
clock.children[2].innerHTML = s0 + ss;
}
function clockStart(){
if (timerID)
return;
timerID = setInterval(clockOn, 1000);
}
function clockStop(){
clearInterval(timerID);
timerID = null;
}
</script>
</body>
</html>