1.1.1 • Published 7 years ago
interval-loop-manager v1.1.1
Interval Loop Manager
A class to manage a loop with some interval.
Purpose
I want set a loop with some interval.
Sometimes, I want to stop the loop or change the interval.
How to use
Setup
Require interval-loop-manager.
const IntervalLoopManager = require('interval-loop-manager')
I explain interval loop manager by setting a loop of the following function.
const yourFunction = () => {
// some process
}
Start a loop
In case you want to set loop with 5 seconds interval.
const managedLoop =
new IntervalLoopManager(
yoruFunction,
{ interval: 5000 }
)
or
var managedLoop = new IntervalLoopManager( yourFunction )
managedLoop.start({ interval: 5000 })
Stop the loop
managedLoop.stop()
Restart the loop
managedLoop.restart()
or
managedLoop.start()
Change the interval
Ways to change the interval to 7 seconds.
In case you want to change it as soon as possible.
managedLoop.restart({ interval: 7000 })
In case you allow current loop to finish.
managedLoop.assignValues({ interval: 7000 })
Detect whether the loop is running or not
if ( managedLoop.isRunning() ) {
console.log('Loop is running.')
} else {
console.log('Loop is not running.')
}
License
MIT
Reference
http://stackoverflow.com/questions/1280263/changing-the-interval-of-setinterval-while-its-running