-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
58 lines (51 loc) · 1.11 KB
/
index.js
File metadata and controls
58 lines (51 loc) · 1.11 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
'use strict';
const convert = (time) => {
const toTwoDigit = d => Math.floor(d / 10) + '' + (d % 10);
const h = Math.floor(time / (60 * 60));
const hh = toTwoDigit(h);
let m = Math.floor(time / 60);
m = ((m % 60) + 60) % 60;
const mm = toTwoDigit(m);
const s = time % 60;
const ss = toTwoDigit(s);
return {
hh,
h,
mm,
m,
ss,
s,
};
};
const createCountdown = ({ h, m, s } = {}, { listen = () => { }, done = () => { } }) => {
let limit = 0;
let start = 0;
let interval;
const set = ({ h = 0, m = 0, s = 0 }) => {
limit = h * 60 * 60 + m * 60 + s;
};
set({ h, m, s });
return {
start: () => {
listen(convert(limit - start));
interval = setInterval(() => {
start += 1;
listen(convert(limit - start));
if (start === limit) {
done();
clearInterval(interval);
}
}, 1000);
},
reset: () => {
start = 0;
listen(convert(limit - start));
clearInterval(interval);
},
stop: () => {
clearInterval(interval);
},
set,
};
};
export default createCountdown;