Miss any of our Open RFC calls?Watch the recordings here! »

@libshin/interval

1.0.4 • Public • Published

TL;DR

Create flexible intervals

How to use

import interval from "@libshin/interval";
 
// interval(fn: () => ?number, initialTTL: number, options)
const timer = interval(fn, 500);
 
// The timer is defined
 
timer.start(); // Start it (1st call of fn in 500ms)
timer.startNow(); // Start timer and immediately calls fn
timer.stop(); // When the timer is running
timer.restart(); // Stops timer and calls start()
timer.restartNow(); // Stops timer and calls startNow()

Why flexible?

The main difference with regular setTimeout in JS is that if the callback function fn returns a number, it will be the new time interval before the next call of fn.

import interval from "@libshin/interval";
 
const fn = () => {
  console.log('fn is called')
  return 1500;
}
 
const timer = interval(fn, 500);
 
timer.start()
 
> "fn is called" // after 500ms
> "fn is called" // after 1500ms
> "fn is called" // after 1500ms
> "fn is called" // after 1500ms
...

Furthermore, start and restart can take a initial ttl:

import interval from "@libshin/interval";
 
const fn = () => {
  console.log('fn is called')
}
 
const timer = interval(fn, 500);
 
timer.start(1500)
 
> "fn is called" // after 1500ms
> "fn is called" // after 500ms
> "fn is called" // after 500ms
> "fn is called" // after 500ms
...

Options

Field Value Default Description
ignoreErrors boolean true If set to true, when the function fn fails, the timer will continue, otherwise, it will stop
errorCb error => void console.error Function that is called when function fn fails if ignoreErrors is set to true

Install

npm i @libshin/interval

DownloadsWeekly Downloads

2

Version

1.0.4

License

MIT

Unpacked Size

81.7 kB

Total Files

10

Last publish

Collaborators

  • avatar
  • avatar