-
Notifications
You must be signed in to change notification settings - Fork 10
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 7b21ec9
Showing
4 changed files
with
182 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,21 @@ | ||
The MIT License (MIT) | ||
|
||
Copyright (c) 2016 Mathias Rasmussen | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
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,67 @@ | ||
# tiny-timer [![dependencies][dep-img]][dep-url] [![license][lic-img]][lic-url] | ||
|
||
[dep-img]: https://david-dm.org/mathiasvr/tiny-timer.svg | ||
[dep-url]: https://david-dm.org/mathiasvr/tiny-timer | ||
[lic-img]: http://img.shields.io/:license-MIT-blue.svg | ||
[lic-url]: http://mvr.mit-license.org | ||
|
||
Small countdown timer and stopwatch module. | ||
|
||
## install | ||
```bash | ||
npm install tiny-timer | ||
``` | ||
|
||
## example | ||
```javascript | ||
const Timer = require('tiny-timer') | ||
|
||
let timer = new Timer({ interval: 200 }) | ||
|
||
timer.on('tick', (ms) => console.log('tick', ms)) | ||
timer.on('done', () => console.log('done!')) | ||
|
||
timer.start(3000) // run for 3 seconds | ||
``` | ||
|
||
# usage | ||
|
||
## `timer = new Timer([options])` | ||
Optional `options`: | ||
```javascript | ||
{ | ||
interval: Number, // refresh interval in ms | ||
stopwatch: Boolean // stopwatch mode, default is countdown | ||
} | ||
``` | ||
|
||
### `timer.start(duration)` { | ||
Starts timer running for a `duration` specified in ms. | ||
|
||
### `timer.stop()` | ||
Stops timer. | ||
|
||
### `timer.pause()` | ||
Pauses timer. | ||
|
||
### `timer.resume()` | ||
Resumes timer. | ||
|
||
## events | ||
|
||
### `timer.on('tick', (ms) => {})` | ||
Event emitted every `interval` with the current time in ms. | ||
|
||
### `timer.on('done', () => {})` | ||
Event emitted when the timer reaches the `duration` set by calling `timer.start()`. | ||
|
||
## properties | ||
|
||
### `timer.time` | ||
Gets the current time in ms. | ||
|
||
### `timer.duration` | ||
Gets the total `duration` the timer is running for in ms. | ||
|
||
### `timer.status` | ||
Gets the current status of the timer as a string: `running`, `paused` or `stopped`. |
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,70 @@ | ||
const EventEmitter = require('events') | ||
|
||
function tick () { | ||
if (this._status === 'paused') return | ||
if (Date.now() >= this._endTime) { | ||
this.stop() | ||
this.emit('tick', this._stopwatch ? this._duration : 0) | ||
this.emit('done') | ||
} else { | ||
this.emit('tick', this.time) | ||
} | ||
} | ||
|
||
class Timer extends EventEmitter { | ||
constructor (options) { | ||
super() | ||
this._interval = options && options.interval || 1000 | ||
this._stopwatch = options && options.stopwatch || false | ||
this._endTime = 0 | ||
this._pauseTime = 0 | ||
this._duration = null | ||
this._timeoutID = null | ||
this._status = 'stopped' // 'running' or 'paused' | ||
} | ||
|
||
start (duration) { | ||
if (this._status !== 'stopped') return | ||
if (duration == null) throw new TypeError('must provide duration parameter') | ||
this._duration = duration | ||
this._endTime = Date.now() + duration | ||
this._status = 'running' | ||
this.emit('tick', this._stopwatch ? 0 : this._duration) | ||
this._timeoutID = setInterval(tick.bind(this), this._interval) | ||
} | ||
|
||
stop () { | ||
clearInterval(this._timeoutID) | ||
this._status = 'stopped' | ||
} | ||
|
||
pause () { | ||
if (this._status !== 'running') return | ||
this._pauseTime = Date.now() | ||
this._status = 'paused' | ||
} | ||
|
||
resume () { | ||
if (this._status !== 'paused') return | ||
this._endTime += Date.now() - this._pauseTime | ||
this._pauseTime = 0 | ||
this._status = 'running' | ||
} | ||
|
||
get time () { | ||
if (this._status === 'stopped') return 0 | ||
if (this._status === 'paused') return this._endTime - this._pauseTime | ||
let left = this._endTime - Date.now() | ||
return this._stopwatch ? this._duration - left : left | ||
} | ||
|
||
get duration () { | ||
return this._duration | ||
} | ||
|
||
get status () { | ||
return this._status | ||
} | ||
} | ||
|
||
module.exports = Timer |
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,24 @@ | ||
{ | ||
"name": "tiny-timer", | ||
"version": "1.0.0", | ||
"description": "Small countdown timer and stopwatch module.", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "standard" | ||
}, | ||
"keywords": [ | ||
"countdown", | ||
"stopwatch", | ||
"timer" | ||
], | ||
"author": "Mathias Rasmussen <[email protected]>", | ||
"license": "MIT", | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/mathiasvr/tiny-timer.git" | ||
}, | ||
"bugs": { | ||
"url": "https://github.com/mathiasvr/tiny-timer/issues" | ||
}, | ||
"homepage": "https://github.com/mathiasvr/tiny-timer#readme" | ||
} |