-
Notifications
You must be signed in to change notification settings - Fork 0
/
timer.js
35 lines (35 loc) · 1.1 KB
/
timer.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
import { differenceInSeconds } from "date-fns";
import inquirer from "inquirer";
const res = await inquirer.prompt({
type: "number",
name: "userInput",
message: "please enter amount of second",
validate: (input) => {
if (isNaN(input)) {
return "please enter valid number";
}
else if (input > 60) {
return "seconds must be in 60";
}
else {
return true;
}
}
});
let input = res.userInput;
function startTime(val) {
const intTime = new Date().setSeconds(new Date().getSeconds() + val);
const IntervalTime = new Date(intTime);
setInterval(() => {
const currentTime = new Date();
const timeDiff = differenceInSeconds(IntervalTime, currentTime);
if (timeDiff <= 0) {
console.log("Timer has Expired");
process.exit();
}
const min = Math.floor((timeDiff % (3600 * 24)) / 3600);
const sec = Math.floor(timeDiff % 60);
console.log(`${min.toString().padStart(2, "0")}:${sec.toString().padStart(2, "0")}`);
}, 1000);
}
startTime(input);