-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 8082e44
Showing
3 changed files
with
95 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
//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 = this.lastTime - now; | ||
const difference = (this.destination) / (this.last - current); | ||
|
||
const timeToComplete = timeDifference * difference; | ||
|
||
const seconds = Math.abs(timeToComplete / 1000); | ||
const minutes = Math.abs(timeToComplete / 1000 / 60); | ||
const hours = Math.abs(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 | ||
} | ||
} | ||
} | ||
|
||
const time = new TimeToComplete(100); | ||
|
||
setTimeout(() => { | ||
time.remaining(40, true); | ||
}, 1000) | ||
|
||
setTimeout(() => { | ||
time.remaining(44, true); | ||
}, 2000) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
{ | ||
"name": "timetocomplete", | ||
"version": "1.0.0", | ||
"description": "A package that'll help you determin how long a process is going to take to complete", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "npm test" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "git+ssh://git@github.com/Streek/TimeToComplete.git" | ||
}, | ||
"author": "Keith Connolly", | ||
"license": "MIT", | ||
"bugs": { | ||
"url": "https://github.com/Streek/TimeToComplete/issues" | ||
}, | ||
"homepage": "https://github.com/Streek/TimeToComplete#readme" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
# TimeToComplete | ||
|
||
This library exported as a class module. | ||
|
||
## Download | ||
|
||
* [Master build](https://github.com/Streek/TimeToComplete) | ||
* [1.0 build](https://github.com/Streek/TimeToComplete/releases) | ||
|
||
TimeToComplete is released under the [MIT license](https://raw.githubusercontent.com/streek/TimeToComplete/LICENSE) & supports modern environments. | ||
|
||
## Installation | ||
|
||
In a browser: | ||
```html | ||
<script src="TimeToComplete/index.js"></script> | ||
``` | ||
|
||
Using npm: | ||
```shell | ||
$ npm i -g npm | ||
$ npm i --save TimeToComplete | ||
``` | ||
|
||
In Node.js: | ||
```js | ||
// Load the full build. | ||
const timeClass = require('TimeToComplete'); | ||
const time = new timeClass; | ||
``` |