-
Notifications
You must be signed in to change notification settings - Fork 3
/
utils.js
41 lines (34 loc) · 1.12 KB
/
utils.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
const moment = require('moment');
const readline = require('readline');
const delay = ms => new Promise((r, j) => setTimeout(r, ms));
const log = msg => console.log(`[${moment().format('HH:mm:ss.SSS')}] ${msg}`);
// startTime is the start time of the event in milliseconds.
const displayCountdown = startTime => {
const formatNumber = number => {
if (number < 10) {
return `0${number}`;
} else {
return number;
}
}
let interval = setInterval(() => {
if (moment().valueOf() >= startTime) {
clearInterval(interval);
} else {
let secondsToGo = Math.round((startTime / 1000)) - moment().unix();
readline.clearLine(process.stdout, 0);
readline.cursorTo(process.stdout, 0);
process.stdout.write(
`${formatNumber(Math.floor(secondsToGo/(60*60)))}:` +
`${formatNumber(Math.floor( (secondsToGo/60) % 60 ))}:` +
`${formatNumber(Math.floor(secondsToGo % 60))}`
);
}
},
1000);
}
module.exports = {
delay,
log,
displayCountdown
};