-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtimer.ts
More file actions
38 lines (34 loc) · 1.08 KB
/
timer.ts
File metadata and controls
38 lines (34 loc) · 1.08 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
#! /usr/bin/env node
import { differenceInSeconds, min } from "date-fns";
import inquirer from "inquirer";
const res = await inquirer.prompt({
type: "number",
name:"userInput",
message: "Please Enter the amount of second",
validate : (input) =>{
if(isNaN(input)){
return "enter the number"
}else if(input > 60) {
return "Seconds must be in 60"
}else {
return true;
}
}
})
let input = res.userInput
function startTimer(val:number){
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('Time 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
}
startTimer(input)