-
Notifications
You must be signed in to change notification settings - Fork 0
/
stopwatch.html
80 lines (78 loc) · 1.96 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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
.container{
width: 90%;
max-width: 600px;
background-image: linear-gradient(rgba(0,0,0,0.4),rgba(0,0,0,0.4)),url("https://wallpapercave.com/wp/wp2869302.jpg");
background-size: cover;
background-position: center;
text-align: center;
padding: 60px 0;
color: #fff;
margin: 200px auto 0;
box-shadow: 0 10px 10px rgba(0,0,0,0.8);
}
button{
outline: none;
border: none;
background-color: transparent;
color: #fff;
font-size: 17px;
font-style: italic;
padding: 7px 9px;
margin: 4px;
font-weight: 900;
cursor: pointer;
}
</style>
</head>
<body>
<div class="container">
<h1 id="time">00:00:00</h1>
<div class="buttons">
<button onclick="watchStop()">Stop</button>
<button onclick="startWatch()">Start</button>
<button onclick="resetWatch()">Reset</button>
</div>
</div>
<script>
let time = document.getElementById("time");
let [seconds, minutes, hours] = [0,0,0];
let timer = null;
function stopWatch(){
seconds++;
if(seconds === 60){
seconds = 0;
minutes++;
}
if(minutes === 60){
minutes = 0;
hours++;
}
let h = hours < 10 ? "0" + hours:hours;
let m = minutes < 10 ? "0" + minutes:minutes;
let s = seconds < 10 ? "0" + seconds:seconds;
time.innerHTML = h+ ":" + m + ":" + s;
}
function startWatch(){
if(timer!= null){
clearInterval(timer);
}
timer = setInterval(stopWatch,1000);
}
function watchStop(){
clearInterval(timer);
}
function resetWatch(){
clearInterval(timer);
[seconds, minutes, hours] = [0,0,0];
time.innerHTML = "00:00:00";
}
</script>
</body>
</html>