forked from muddhit/Hack-with-JavaScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
60 lines (50 loc) · 1.03 KB
/
script.js
File metadata and controls
60 lines (50 loc) · 1.03 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
var ms = 0, s = 0, m = 0;
var timer;
var stopwatchEl = document.querySelector('.stopwatch');
var lapsContainer = document.querySelector('.laps');
function start() {
if (!timer) {
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;
ms = 0;
s = 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(){
var li = document.createElement('li');
li.innerText = getTimer();
lapsContainer.appendChild(li);
}
function resetLaps(){
lapsContainer.innerHTML = '';
}