-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstopwatchscript.js
92 lines (76 loc) · 2.03 KB
/
stopwatchscript.js
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
let hours = 00;
let minutes = 00;
let seconds = 00;
let milliseconds = 00;
let interval;
const hoursContainer = document.getElementById("hours");
const minutesContainer = document.getElementById("minutes");
const secondsContainer = document.getElementById("seconds");
const millisecondsContainer = document.getElementById("milliseconds");
const start= document.getElementById("start");
const stop= document.getElementById("stop");
const reset= document.getElementById("reset");
const startTimer =() => {
clearInterval(interval);
interval = setInterval(startWatch,1);
}
const stopTimer =() => {
clearInterval(interval);
}
const resetTimer =() => {
milliseconds=0;
seconds=0;
minutes=0;
hours=0;
millisecondsContainer.innerHTML = "00";
secondsContainer.innerHTML = "00";
minutesContainer.innerHTML = "00";
hoursContainer.innerHTML = "00";
clearInterval(interval);
}
function startWatch(){
milliseconds++;
if(milliseconds < 9){
millisecondsContainer.innerHTML= `0${milliseconds}`;
}
else if(milliseconds > 99){
seconds++;
milliseconds=0;
millisecondsContainer.innerHTML= "00";
}
else{
millisecondsContainer.innerHTML = milliseconds;
}
if(seconds < 10){
secondsContainer.innerHTML= `0${seconds}`;
}
else if(seconds > 59){
minutes++;
seconds=0;
secondsContainer.innerHTML= "00";
}
else{
secondsContainer.innerHTML = seconds;
}
if(minutes < 10){
minutesContainer.innerHTML= `0${minutes}`;
}
else if(minutes > 59){
hours++;
minutes=0;
minutesContainer.innerHTML= minutes;
}
else{
minutesContainer.innerHTML = minutes;
}
if(hours < 10){
hoursContainer.innerHTML= `0${hours}`;
}
else{
hoursContainer.innerHTML = hours;
}
}
start.addEventListener("click",startTimer);
stop.addEventListener("click",stopTimer);
reset.addEventListener("click",resetTimer);
//Timer