-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
56 lines (47 loc) · 1.26 KB
/
app.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
const output = document.getElementById('output');
const startBtn = document.getElementById('start');
const pauseBtn = document.getElementById('pause');
const resetBtn = document.getElementById('reset');
let tick = 0;
let timeout = 10;
let mode = 'start';
startBtn.onclick = bindMode('start');
pauseBtn.onclick = bindMode('pause');
resetBtn.onclick = bindMode('reset');
function bindMode(command) {
return () => mode = command;
}
let timerId = setTimeout(function update() {
output.textContent = format();
checkMode();
timerId = setTimeout(update, timeout);
}, timeout);
function checkMode() {
if (mode === 'start') {
tick += timeout;
return;
}
if (mode === 'pause') {
clearTimeout(timerId);
return;
}
if (mode === 'reset') {
tick = 0;
return;
}
}
function format() {
const ms = parseInt(tick / 10);
const sec = parseInt(ms / 100);
const min = parseInt(sec / 60);
const hrs = parseInt(min / 60);
const H = addLeadZero(hrs % 24);
const M = addLeadZero(min % 60);
const S = addLeadZero(sec % 60);
const Ms = addLeadZero(ms % 60);
console.log(`hrs: ${H}\n min: ${M}\n sec: ${S}\nms: ${Ms}`);
return `${H}:${M}:${S}.${Ms}`;
}
function addLeadZero(num) {
return (num < 10) ? '0' + num : num;
}