Helper for creating auto-calling functions


stars 🌟issues ⚠️updated 🛠created 🐣size 🏋️‍♀️
02Jun 6, 2021Mar 9, 2018Minified + gzip package size for @antongolub/repeater in KB



Helper for creating auto-calling functions

buildStatus npm (tag) dependencyStatus devDependencyStatus Maintainability Test Coverage js-standard-style


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 example
    import repeater from '@antongolub/repeater'
    const target = step => { this.i += step }
    const context = { i: 0 }
    const delay = 1000
    const rep = repeater(target, delay, context)
    // Imagine, 5 seconds later new 'manual' call occurs
    setTimeout(() => rep(1), 5000)

    // ~10 seconds after start: 
    setTimeout(() => console.log(context.i), 10000) // 15

Repeater is just a wrapper around the target function. It exposes several util props:

Prop Description
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:

    const rep1 = repeater(target, delay, context, limit)
    const rep2 = repeater({target, delay, context, limit})

If you find any bugs or have a feature request, please open an issue on github!

The npm package download data comes from npm's download counts api and package details come from npms.io.