-
Notifications
You must be signed in to change notification settings - Fork 98
Expand file tree
/
Copy pathindex.js
More file actions
98 lines (84 loc) · 2.74 KB
/
index.js
File metadata and controls
98 lines (84 loc) · 2.74 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
const chronometer = new Chronometer();
// Get the buttons:
const btnLeftElement = document.getElementById('btnLeft');
const btnRightElement = document.getElementById('btnRight');
// Get the DOM elements to display time:
const minDecElement = document.getElementById('minDec');
const minUniElement = document.getElementById('minUni');
const secDecElement = document.getElementById('secDec');
const secUniElement = document.getElementById('secUni');
const milDecElement = document.getElementById('milDec');
const milUniElement = document.getElementById('milUni');
const splitsElement = document.getElementById('splits');
function printTime() {
printMinutes();
printSeconds();
// Optional if you're doing milliseconds
// printMilliseconds();
}
function printMinutes() {
const minutes = chronometer.getMinutes();
const twoDigitMinutes = chronometer.computeTwoDigitNumber(minutes);
minDecElement.innerText = twoDigitMinutes[0];
minUniElement.innerText = twoDigitMinutes[1];
}
function printSeconds() {
const seconds = chronometer.getSeconds();
const twoDigitSeconds = chronometer.computeTwoDigitNumber(seconds);
secDecElement.innerText = twoDigitSeconds[0];
secUniElement.innerText = twoDigitSeconds[1];
}
// BONUS: Milliseconds (only if you're doing it)
function printMilliseconds() {
const milliseconds = chronometer.getMilliseconds(); // Make sure this method exists
const twoDigitMilliseconds = chronometer.computeTwoDigitNumber(milliseconds);
milDecElement.innerText = twoDigitMilliseconds[0];
milUniElement.innerText = twoDigitMilliseconds[1];
}
function printSplit() {
const splitTime = chronometer.split();
const splitElement = document.createElement('li');
splitElement.innerText = splitTime;
splitsElement.appendChild(splitElement);
}
function clearSplits() {
splitsElement.innerHTML = '';
}
function setStopBtn() {
btnLeftElement.className = 'btn stop';
btnLeftElement.innerText = 'STOP';
}
function setSplitBtn() {
btnRightElement.className = 'btn split';
btnRightElement.innerText = 'SPLIT';
}
function setStartBtn() {
btnLeftElement.className = 'btn start';
btnLeftElement.innerText = 'START';
}
function setResetBtn() {
btnRightElement.className = 'btn reset';
btnRightElement.innerText = 'RESET';
}
// Start/Stop Button
btnLeftElement.addEventListener('click', () => {
if (btnLeftElement.classList.contains('start')) {
chronometer.start(printTime);
setStopBtn();
setSplitBtn();
} else {
chronometer.stop();
setStartBtn();
setResetBtn();
}
});
// Reset/Split Button
btnRightElement.addEventListener('click', () => {
if (btnRightElement.classList.contains('reset')) {
chronometer.reset();
clearSplits();
printTime(); // Resets the display too
} else {
printSplit();
}
});