-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
62 lines (60 loc) · 1.66 KB
/
index.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
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href="styles.css" rel="stylesheet"></link>
<title>JavaScript Stopwatch</title>
</head>
<body>
<div class="stopwatch">
<div id="hours">00</div>:
<div id="minutes">00</div>:
<div id="seconds">00</div>
</div>
<div class="buttons">
<button id="start" onclick="startWatch()">Start</button>
<button id="stop" onclick="stopWatch()">Stop</button>
<button id="clear" onclick="clearWatch()">Clear</button>
</div>
<script>
var seconds = 0, minutes = 0, hours = 0, timerId = '';
function startWatch(){
console.log('start');
timerId = setInterval(updateTime,1000);
}
function stopWatch(){
console.log('stop');
clearInterval(timerId);
}
function clearWatch(){
console.log('clear');
seconds = 0;
minutes = 0;
hours = 0;
clearInterval(timerId);
displayNewTime();
}
function updateTime(){
seconds++;
if(seconds >= 60){
seconds = 0;
minutes++;
if(minutes >=60){
minutes = 0;
hours++;
}
}
displayNewTime();
}
function displayNewTime(){
document.getElementById('hours').innerHTML = padDigits(hours,2);
document.getElementById('minutes').innerHTML = padDigits(minutes,2);
document.getElementById('seconds').innerHTML = padDigits(seconds,2);
}
function padDigits(number, digits) {
return Array(Math.max(digits - String(number).length + 1, 0)).join(0) + number;
}
</script>
</body>
</html>