1.0.1 • Published 2 years ago

stateful-timeout v1.0.1

Weekly downloads
-
License
ISC
Repository
-
Last release
2 years ago

stateful-timeout

stateful setTimeout and setInterval

reuse and refresh timeouts and intervals without duplication.

Install

$ npm install stateful-timeout

Usage (timeout)

const {timeout} = require('stateful-timeout')

var f = ()=>console.log('hello world')
var t = new timeout(f,1000)
// => 1000 // after 1s

t.stop()
// // output cancelled

t.refresh()
// => 1000 //after 1s, resets internal timer to 0

t.continue()
// => 1000 //after 1s, continues from current timer

Usage (interval)

const {interval} = require('stateful-timeout')

var f = ()=>console.log('hello world')
let i = new interval(f,1000)
// => 1000 //every 1s

i.stop()
// // output stops

i.refresh()
// => 1000 //every 1s, resets internal timer to 0

i.continue()
// => 1000 //every 1s, continues from current timer

Usage Notes

continue() and refresh() work fine after calling stop()

continue() and refresh() can be called any number of times without creating extra timeouts or intervals