README
Timer
Minimal javascript library to create and manage timers
NOTE: Depends on window.requestAnimationFrame
. If your environment does not support it, you can polyfill.
Install
$ npm install @georapbox/timer --save
Usage
The library is exported in UMD, CommonJS, and ESM formats. You can import it the following ways:
Using ESM import statement
import Timer from '@georapbox/timer';
Using CommonJS require statement
const Timer = require('@georapbox/timer').default;
As old school browser global
<script src="https://unpkg.com/@georapbox/timer"></script>
API
- Timer
- new Timer([duration], [callback])
- .time() ⇒
Object
- .start([shouldReset]) ⇒
Timer
- .stop() ⇒
Timer
- .reset([shouldStop]) ⇒
Timer
- .isRunning() ⇒
Boolean
new Timer([duration], [callback])
Creates a Timer instance.
Param | Type | Description |
---|---|---|
[duration] | Number |
The timer's duration (ms). If left undefined or 0 or negative number the timer counts up instead of down. |
[callback] | function |
Function to be executed while timer is running. The Timer instance is passed by as parameter. |
Object
timer.time() ⇒ Get the remaining and elapsed time.
If no duration is specified during initialization, the remaining time will always be 0
.
Kind: instance method of Timer
Returns: Object
- An object that contains the remaining and the elapsed time in milliseconds.
Timer
timer.start([shouldReset]) ⇒ Start the timer.
If the timer instance has been already started, the timer will just resume.
Kind: instance method of Timer
Returns: Timer
- The Timer instance.
Param | Type | Description |
---|---|---|
[shouldReset] | Boolean |
If set to true , the timer will reset to initial specified duration. |
Timer
timer.stop() ⇒ Stop/Pause the timer.
Kind: instance method of Timer
Returns: Timer
- The Timer instance.
Timer
timer.reset([shouldStop]) ⇒ Resets the timer to initial specified duration.
Kind: instance method of Timer
Returns: Timer
- The Timer instance.
Param | Type | Description |
---|---|---|
[shouldStop] | Boolean |
If set to true , the timer will be forced to stop; otherwise will reset and continue running. |
Boolean
timer.isRunning() ⇒ Check (at any time) if the timer is running or not.
Kind: instance method of Timer
Returns: Boolean
- True if the timer is running; otherwise false.
Test
$ npm run test