-
Notifications
You must be signed in to change notification settings - Fork 0
/
javascript.js
66 lines (55 loc) · 1.09 KB
/
javascript.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
let m = 0;
let s = 0;
let ms = 0;
let timer;
let stopwatchEl = document.querySelector('.stopwatch');
let lapsContainer = document.querySelector('.laps');
function start(){
console.log(!timer);
if(!timer){ //so if you press start button many times, there wont be a glitch
timer = setInterval(run , 10);
}
}
function run(){
stopwatchEl.textContent = getTimer();
ms++;
if(ms === 100){
ms = 0;
s++;
}
if(s === 60){
s = 0;
m++;
}
}
function pause(){
stopTimer();
}
function stop(){
stopTimer();
m = 0;
s = 0;
ms = 0;
stopwatchEl.textContent = getTimer();
}
function stopTimer(){
clearInterval(timer);
timer = false;
}
function getTimer(){
return (m < 10 ? "0" + m : m) + ":" + (s < 10 ? "0" + s : s) + ":" + (ms < 10 ? "0" + ms : ms);
}
function restart(){
stop();
start();
}
function lap() {
if(timer){
let li = document.createElement("li");
li.innerText = getTimer();
lapsContainer.appendChild(li);
}
}
function resetLaps(){
lapsContainer.innerHTML = "";
}