setinterval-plus
setinterval-plus is inspired by the stackoverflow answer on how to pause and resume normal setInterval timers.
What setinterval-plus provides is a smiliar API as the vanila function setIntreval only the object returned isn't a Timer but instead it returns and object with the ability to start/pause/resume/stop the Timer object with a very intuitive and foolproof API.
Installation
$ npm i setinterval-plus
Create a new setinterval-plus
After creating a new instance the timer will start running.
Params:
- callback - The function to be called on each setInterval iteration. Can't take any args since the function will be called like this: callback()
- interval - Time in ms to wait between intervals
Example:
var callback = function () {
console.log('Run')
}
var timer = new _setInterval(callback, 1000)
API
timer.pause()
timer.resume()
timer.stop()
timer.start()
Example:
var _setInterval = require('setinterval-plus')
var runs = 0
var timer = new _setInterval(function () {
console.log('Run')
runs++
}, 100)
setTimeout(function () {
timer.pause()
setTimeout(function () {
console.log('Remaining time for Interval: ', timer.remaining)
timer.resume()
setTimeout(function () {
timer.stop()
setTimeout(function () {
timer.start()
console.log('Remaining time for Interval: ', timer.remaining)
var check = new Date()
setTimeout(function () {
console.log('Time before rerunning', new Date() - check)
console.log(runs)
}, 1045)
}, 5)
}, 200)
}, 100)
}, 350)
Development
$ git clone git@github.com:thehobbit85/setinterval-plus.git
$ cd setinterval-plus
$ npm i
$ npm test
$ npm run coverage
License
MIT