intervals-composite
Encapsulate javascript .setInterval
& .clearInterval
into an Interval class. It also adds an IntervalComposite that simplifies working with multiple intervals in an application.
Table of Contents
install
npm install --save intervals-composite
API
require
const Interval IntervalComposite = ;
import
;
Interval
Construction
constructor(params) | ||
---|---|---|
name | type | props |
params | object |
cb function ms number label string |
Example
const interval = console ms: 3000 label: 'test-interval'; // OR, if you have the callback in another object const handler = console; const interval = cb: handlersomeFunction ms: 3000 label: 'test-interval';
.getLabel()
gets the interval label.
return |
---|
string |
Example
console; // 'test-interval'
.getMs()
gets the interval ms.
return |
---|
number |
Example
console; // 3000
.getCb()
gets the interval callback.
return |
---|
function |
Example
console; // [Function: someFunction]
.start()
starts the interval.
Example
intervalstart;
.isRunning()
checks if the interval is running.
Example
console; // true /*testtesttest...*/
.clear()
clears the interval
Example
intervalclear; console; // false
IntervalComposite
Construction
constructor(label) | |
---|---|
name | type |
label | string |
Example
const dataLoaders = 'data-loaders';
.add(interval)
adds an interval.
params | |
---|---|
name | type |
interval | Interval |
Example
dataLoaders; dataLoaders; dataLoaders;
.has(label)
checks if a label exists.
params | |
---|---|
name | type |
label | string |
Example
console; // true
.get(label)
gets an interval by its label.
return |
---|
Interval |
Example
const ordersInterval = dataLoaders; console; // ordersconsole; // false
.getLabel()
gets the composite label.
return |
---|
string |
Example
console; // data-loaders
.forEach(cb)
traverses the intervals.
params | |
---|---|
name | type |
cb | function |
Example
dataLoaders; /*usersproductsorders*/
.filter(cb, label)
filters the intervals using a callback. It also accept an optional label to name the filtered composite.
params | |
---|---|
name | type |
cb | function |
label | string |
Example
const slowLoaders = dataLoaders; console; // slow-intervals slowLoaders;/*usersproducts*/
.toArray()
converts the composite into an array of intervals.
return | item type |
---|---|
array | Interval |
Example
console; /*[ 'users', 'products', 'orders' ]*/
.count()
gets the count of intervals.
return |
---|
number |
Example
console; // 3
.start()
starts the intervals
Example
dataLoadersstart; /*ordersordersproductsordersordersordersproductsordersusersordersorders*/
.isRunning()
checks if the intervals are started.
return |
---|
boolean |
Example
console; // true
.clear()
clears the intervals.
Example
dataLoadersclear;console; // false
Build
grunt build
License
The MIT License. Full License is here