timergroup v0.1.1
TimerGroup
A simple module for grouping timers and their outputs together using Promises.
No seriously, it's really quite simple.
Install
Using npm, module loaders, or manually grab and include it in your project.
$ npm install timergroup
Node
var TimerGroup = require('timergroup');
AMD
require(['TimerGroup'], function (TimerGroup) {});
Manual (Web)
<script src="TimerGroup.js"></script>
You'll figure it out.
Usage
var timers = new TimerGroup();
timers
.add(Date.now, 500)
.add(Date.now, 1500)
.add(Date.now, 1000)
.cancel(-2)
.add(Date.now, 2000)
.done()
.then(function (results) {
console.log(results); // Array [ 1450308566947, null, 1450308567447, 1450308568447 ]
});
Instance properties
.ids
: Array of setTimeout IDs..self
: Array of Promise objects..finished {false}
: Boolean, whether or not all Promises have been resolved. Only changes after.done
has been called at least once.
Prototype methods
.add(function, delay)
: Creates a new promisified timer. Returns the instance.
.cancel(index)
: Cancels a timer, and nullifies the Promise. Default is the last timer created (index = -1
). Indices wrap, and may be in reverse. Returns the instance.
.done()
: Returns a Promise which resolves when all timers have resolved or been cancelled. When resolved the .finished
property is set to true
. Data passed through is an array containing the return values of each timer's function, in the order created - null
in the case of a cancelled timer.
Enjoy!