@antongolub/repeater
Helper for creating auto-calling functions

Motivation
The wheel was invented a long time ago: repeat.
What's the diff?
- Repeater stores the last call params and uses them for next invocations.
- Inherits target's iface.
- Allows to combine
manual and automated calls.
Usage
import repeater from '@antongolub/repeater'
const target = step => { this.i += step }
const context = { i: 0 }
const delay = 1000
const rep = repeater(target, delay, context)
rep(2)
setTimeout(() => rep(1), 5000)
setTimeout(() => console.log(context.i), 10000)
Repeater is just a wrapper around the target function. It exposes several util props:
delay | interval in ms |
timeout | TimeoutID |
target | ref for original target function |
context | optional scope |
limit | optional remainder of calls |
args | arguments of the last invocation |
So, anytime you're let to interrupt the repetitive call by clearing timeout:
clearTimeout(rep.timeout)
Parametrization
const rep1 = repeater(target, delay, context, limit)
const rep2 = repeater({target, delay, context, limit})
License
MIT