A moment.js plugin for timers setInterval & setTimeout.
npm install moment-timer[![NPM version][npm-version-image]][npm-url] [![NPM downloads][npm-downloads-image]][npm-url] [![MIT License][license-image]][license-url]
---
---
npm install moment-timer
`$3
`
bower install moment-timer
`$3
`
`$3
`
`
When using this plugin in the browser, be sure to include moment.js on your page first.---
Attributes
$3
`js
new moment.duration(1000).timer({ start: true }, callback);
`
Setting this attribute to true will cause the timer to start once instantiated.---
$3
`js
new moment.duration(1000).timer({ loop: true }, callback);
`
Setting this attribute to true will cause the timer to loop/restart once a duration is complete.---
$3
`js
new moment.duration(1000).timer({ wait: 5000 }, callback);
`
`js
new moment.duration(1000).timer({ wait: moment.duration(5, 'seconds') }, callback);
`
Setting this attribute will cause the timer to wait for a specified amount of time before starting it's duration. This is kind of an extra first duration. Imagine a timer that runs every second. Setting the wait attribute to 5000 / 5 seconds, means it waits that long and then starts acting like a normal timer would. Notice that this attribute accepts both int and moment.duration .---
$3
`js
new moment.duration(1000).timer({ wait: 5000, executeAfterWait: true }, callback);
`
Setting this attribute to true will cause the callback function to be called after the wait duration has ended. This is a way to make sure the callback is executed even before the timer starts.---
Functions
$3
`js
let timer = new moment.duration(1000).timer(callback);
timer.start();
`
This function will cause the timer to start. It can be used if the start attribute has not been set or if the timer has been stopped.---
$3
`js
let timer = new moment.duration(1000).timer({ start: true }, callback);
timer.stop();
`
This function will cause the timer to stop. It can be used if timer has been started to halt it.---
$3
`js
let timer = new moment.duration(1000).timer(callback);
timer.duration(5000);
timer.duration(moment.duration(5, "seconds");
`
This function can be used to change the duration the timer was instantiated with.---
$3
`js
let timer = new moment.duration(1000).timer(callback);
timer.getDuration();
`
This function will return the current duration of a timer. In this case it will return 1000.---
$3
`js
let timer = new moment.duration(1000).timer(callback);
timer.getRemainingDuration();
`
This function will return the remaining duration of a timers cycle. In this case, imagine that the timer has been running for 500ms and we call .getRemainingDuration() on it, in this example it will return 500, since half of the cycle has completed.---
$3
`js
let timer = new moment.duration(1000).timer(callback);
timer.start();
timer.isStopped(); // false
timer.stop();
timer.isStopped(); // true
`
This function can be used to see if the timer has been stopped by the .stop() function.---
$3
`js
let timer = new moment.duration(1000).timer(callback);
timer.start();
timer.isStarted(); // true
timer.stop();
timer.isStarted(); // false
``---
Feel free to open a new issue or create a pull request if you can think of other useful attributes or functions.
---
---
You are always welcome to contribute to this repository. Create your own branch, make the changes you wish to see and create a pull request that we can have a look at. If the changes make sense and the quality of code is good enough, then it will be merged into the master branch so other people can use it.
A full list of contributers for moment-timer.js can be found here.
---
Moment-timer.js is freely distributable under the terms of the MIT license.
---
[npm-url]: https://npmjs.org/package/moment-timer
[npm-version-image]: http://img.shields.io/npm/v/moment-timer.svg?style=flat
[npm-downloads-image]: http://img.shields.io/npm/dm/moment-timer.svg?style=flat
[license-image]: http://img.shields.io/badge/license-MIT-blue.svg?style=flat
[license-url]: LICENSE