ealmansi/set-interval-async

Add a way to change the interval

Closed this issue · 1 comments

gajus commented

Working with async iterators, I am finding myself often needing to change interval settings during the life time of the event, i.e. I would like to be able to do this:

const interval = setIntervalAsync(async () => {
  // After the initial 1000 delay, now the interval is set to 100.
  interval.setInterval(100);
}, 1000);

Example use case: Think of a job queue that needs to back-off when there are no tasks, and increase the rate at which it checks for new tasks when there are tasks.

Hi @gajus, thanks for your input. One of the goals for this project is to keep the API as similar as possible to the built-in setInterval API, so such extensions are unlikely to be added.

However, you could achieve something similar like this:

function fn (o) {
  // if we need to change the interval
  clearInterval(o.t);
  o.t = setInterval(fn, 100, o);
}
var o = {};
o.t = setInterval(fn, 1000, o);