-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
57 lines (48 loc) · 1.23 KB
/
script.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
const btnStart=document.querySelector('.start');
const btnStop=document.querySelector('.stop');
const btnReset=document.querySelector('.reset');
let hrs=min=sec=ms=0,startTimer;
btnStart.addEventListener('click',()=>{
btnStart.classList.add('start-active');
btnStop.classList.remove('stop-active');
startTimer=setInterval(()=>{
ms++;//ms=ms+1;
if(ms==100){
sec++;
ms=0;
}
if(sec==60){
min++;
sec=0;
}
if(min==60){
hrs++;
min=0;
}
updateDisplay();
},10);
});
btnStop.addEventListener('click',()=>{
clearInterval(startTimer);
btnStart.classList.remove('start-active');
btnStop.classList.add('stop-active');
});
btnReset.addEventListener('click',()=>{
hrs=min=sec=ms=0;
clearInterval(startTimer);
updateDisplay();
btnStart.classList.remove('start-active');
btnStop.classList.remove('stop-active');
});
function updateDisplay(){
//Formated Display
phrs=hrs<10?'0'+hrs:hrs;
pmin=min<10?'0'+min:min;
psec=sec<10?'0'+sec:sec;
pms=ms<10?'0'+ms:ms;
//Output
document.querySelector('.hrs').innerText=phrs;
document.querySelector('.min').innerText=pmin;
document.querySelector('.sec').innerText=psec;
document.querySelector('.ms').innerText=pms;
}