andyfleming/interval-promise

interval inside interval

Closed this issue · 3 comments

So i'm trying to run a interval inside of a interval. We will call the first interval interval 1 and the interval inside interval 1 will be interval 2. So the problem i'm having is when I try to stop interval 2 it stops both interval 1 and 2. I tried to fix it by doing this

interval(async() => {
  some.stuff.here
  let interval2 = interval(async() => {
    some.stuff.here
    stop(interval2)
  }, 500);
  stop()
}, 500);

I hope you understand when i'm saying.

Closing this as it's not a feature request or bug.

To address your issue, you'll need to name the stop functions differently for each interval context. See modified code below:

interval(async(iterationNumber, stop) => {
  // some.stuff.here
  interval(async(iterationNumber2, stop2) => {
    // some.stuff.here
    stop2()
  }, 500);
  stop()
}, 500);

If you want to wait for the inner interval within each iteration of the outer one, you might also want to await it like below:

interval(async(iterationNumber, stop) => {
  // some.stuff.here
  await interval(async(iterationNumber2, stop2) => {
    // some.stuff.here
    stop2();
  }, 500);
  stop();
}, 500);

Wow, thank you so much!

No problem. Glad I could help!