Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/jasperck/promise-timer-stopwatch
A stopwatch supports promise usage for node.js with 0 dependency
https://github.com/jasperck/promise-timer-stopwatch
module nodejs promise promise-timer-stopwatch stopwatch
Last synced: about 1 month ago
JSON representation
A stopwatch supports promise usage for node.js with 0 dependency
- Host: GitHub
- URL: https://github.com/jasperck/promise-timer-stopwatch
- Owner: jasperck
- License: mit
- Created: 2016-12-28T03:51:25.000Z (about 8 years ago)
- Default Branch: master
- Last Pushed: 2017-01-09T06:22:20.000Z (almost 8 years ago)
- Last Synced: 2024-10-28T16:56:49.489Z (2 months ago)
- Topics: module, nodejs, promise, promise-timer-stopwatch, stopwatch
- Language: JavaScript
- Size: 8.79 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: readme.md
- License: LICENSE
Awesome Lists containing this project
README
Promise-Timer-Stopwatch
=======================
A stopwatch supports **promise** usage for node.js with **0** dependency[![Build Status](https://travis-ci.org/jasperck/promise-timer-stopwatch.svg?branch=master)](https://travis-ci.org/jasperck/promise-timer-stopwatch)
Instalation
-----------
```shell
npm install promise-timer-stopwatch
```Usage
-----------
**Requirement**
```js
const Stopwatch = require('stopwatch');
const stopwatch = new Stopwatch();
```
**Events**
```js
stopwatch.on('started', () => { // when stopwatch started });
stopwatch.on('stopped', () => { // when stopwatch stopped });
stopwatch.on('paused', () => { // when stopwatch paused });
```
**Methods**#### `.start(time)`
Start the stopwatch with duration given, trigger `started` event.
**Arguments**
time: (number; `unit: second`) The duration time stopwatch should watch.
**Return**
(Promise) Resolve when done.
#### `.pause([ time ])`
Pause the stopwatch, update and keep the latest duration left, trigger `paused` event.
**Arguments**
time: (number; optional; default = `0`; `unit: second`) The duration stopwatch should pause, default `0` means stopwatch will pause until `.start()` is called again.
**Return**
(Promise | Self): Promise if argument **time** is given, otherwise return self.
#### `.stop()`
Stop the stopwatch, trigger `stopped` event.
**Return**
(Promise) Resolve when done.
#### `.clear([ clearDuration ])`
Clear the stopwatch job of instance.
**Arguments**
clearDuration: (boolean; optional) Also clear the current duration. usually for `reset`.
**Return**
(Promise) Resolve when done.
#### `.getDuration()`
Get stopwatch current duration.
**Return**
(number) Current duration stopwatch left.
#### `.getStatus()`
Get stopwatch current status.
**Return**
(String) Current status stopwatch is.
**Sample codes**
```js
const bluebird = require('bluebird'); // Using for example// 1.
stopwatch.start(3)
.then(() => { // do what you like })
.then(() => stopwatch.pause(3))
.then(() => stopwatch.clear(true))
.then(() => stopwatch.start(2));// 2.
stopwatch.start(3)
.then(() => { // do what you like })
.then(() => stopwatch.pause())
.then(() => setTimeout(() => stopwatch.start(), 5000));// 3.
stopwatch.start(3)
.then(() => { // do what you like })
.then(() => bluebird.delay(1000))
.then(() => stopwatch.pause())
.then(() => bluebird.delay(3000))
.then(() => stopwatch.start());
```
Test
-----------
```shell
npm test
```
Author
-----------
* Jasper Chang