-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
38 lines (32 loc) · 1.05 KB
/
index.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
//calculate time remaining between now and completion in arbitrary amounts
class TimeToComplete {
constructor(destination) {
this.destination = destination || 100;
this.last = 0;
this.lastTime = Date.now();
}
//calculate time remaining;
remaining(current, consoleLogProgress = false) {
const now = Date.now();
const timeDifference = now - this.lastTime;
const difference = (this.destination) / (current - this.last);
const timeToComplete = timeDifference * difference;
const seconds = timeToComplete / 1000;
const minutes = timeToComplete / 1000 / 60;
const hours = timeToComplete / 1000 / 60 / 60;
if (consoleLogProgress) {
console.log(`This process should be done in ${seconds} seconds.`)
console.log(`This process should be done in ${minutes} minutes.`)
console.log(`This process should be done in ${hours} hours.`)
}
//set for next run;
this.lastTime = now;
this.last = current
return {
seconds,
minutes,
hours
}
}
}
module.exports = TimeToComplete;