-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtimer.html
More file actions
136 lines (117 loc) · 3.42 KB
/
timer.html
File metadata and controls
136 lines (117 loc) · 3.42 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
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=, initial-scale=1.0">
<title>Timer</title>
<style>
body{
font-family: poppins,sans-serif;
}
h1{
text-align: center;
}
.box{
margin: 10rem auto;
padding: 20px;
text-align: center;
border: 3px solid black;
padding: 20px;
width:600px;
border-radius: 10px;
}
#timer{
font-size: 52px;
margin: 2rem;
font-weight: 700;
color: #dd2241;
text-shadow: 2px 2px #be3b51;
}
.buttons{
display: flex;
align-items: center;
justify-content: center;
margin: 1rem;
}
button{
background-color: #dd2241;
padding: 30px 51px;
margin: 1rem;
border: 1px solid #dd2241;
border-radius: 10px;
font-size: 25px;
color: white;
}
button[disabled]{
opacity: 0.5;
cursor: default;
}
</style>
</head>
<body>
<div class="box">
<h1>TIMER</h1>
<div class="timer">
<p id="timer">00:00:00</p>
</div>
<div class="buttons">
<button id="start">Start</button>
<button disabled id="stop">Stop</button>
<button id="reset">Reset</button>
</div>
</div>
<script>
const buttonE1 = document.getElementsByClassName("buttons");
const timerE1 = document.getElementById("timer");
const startE1 = document.getElementById("start");
const stopE1 = document.getElementById("stop");
const resetE1 = document.getElementById("reset");
let startTime = 0;
let elapsedTime = 0;
let timerInterval;
function startTimer(){
// console.log("start");
startTime = Date.now() - elapsedTime;
// console.log(startTime);
timerInterval = setInterval(()=>{
elapsedTime = Date.now() - startTime;
timerE1.innerText= formatTime(elapsedTime);
// console.log(elapsedTime);
},10)
startE1.disabled = true;
stopE1.disabled = false;
}
function formatTime(elapsedTime){
const milliseconds = Math.floor((elapsedTime % 1000)/10);
const seconds = Math.floor((elapsedTime % (1000*60))/1000);
const minutes = Math.floor((elapsedTime % (1000*60*60))/(1000*60));
const hours = Math.floor((elapsedTime % (1000*60*60))/(1000*60));
return (
(hours ? (hours > 9 ? hours : "0" + hours): "00")+
":" +
(minutes ? ( minutes > 9 ? minutes : "0" + minutes):"00")+
":" +
(seconds ? ( seconds > 9 ? seconds : "0" + seconds):"00")+
"."+
(milliseconds > 9 ? milliseconds : "0" + milliseconds)
);
}
function stopTimer(){
// console.log("stop");
clearInterval(timerInterval);
startE1.disabled = false;
stopE1.disabled = true;
}
function resetTimer(){
// console.log("reset");
elapsedTime = 0;
timerE1.innerText="00:00:00"
startE1.disabled = false;
stopE1.disabled = true;
}
startE1.addEventListener("click",startTimer);
stopE1.addEventListener("click",stopTimer);
resetE1.addEventListener("click",resetTimer);
</script>
</body>
</html>