0

I have a condition here which is if a = 6, stop the setInterval so I used clearInterval for my condition, but it doesn't take effect, anybody can help me how I can make the clearInterval work under that condition?

Please take note that in my case makig doSomething to execute after some amount of time is of paramount importance as well, that's why I used setTimeout here.

function doSomething() {

  let a = 1;

  return setInterval(() => {
    if (a < 6) {
      a++;
      console.log(a);
    } else {
      a = 1;
    }
  }, 1000)
}

setTimeout(doSomething, 5000);

var id = doSomething();

if (a === 6) {
  clearInterval(id);
}
  • 1
    `a` is not defined outside of `doSomething`. – Heretic Monkey May 04 '21 at 16:05
  • Does this answer your question? [What is the scope of variables in JavaScript?](https://stackoverflow.com/questions/500431/what-is-the-scope-of-variables-in-javascript) – Heretic Monkey May 04 '21 at 16:05
  • @HereticMonkey No I know the concept of variable scopes, even if I put that `if (a = 6) { clearInterval(id); }` inside the doSomething function, it doesnt work, I just don't know how to make it work. – TheClassyCoder May 04 '21 at 16:09
  • You have to call `clearInterval` inside the interval callback. [Stop setInterval call in JavaScript](https://stackoverflow.com/q/109086) – VLAZ May 04 '21 at 16:09
  • 1
    Off topic - In your if statement you are assigning (`=`) not comparing (`==`) `6` to the variable `a`. `6` is [truthy](https://developer.mozilla.org/en-US/docs/Glossary/Truthy) so the if statement will always execute. – Bronislav Růžička May 04 '21 at 16:09
  • You have two intervals going; the one started in `setTimeout(doSomething, 5000)` and the one started in `var id = doSomething();` Even if `if (a = 6)` was able to access `a` (which it can't), and it used a comparison operator, like `==`, which it doesn't, it would still only clear the second interval . – Heretic Monkey May 04 '21 at 16:10
  • `if (a = 6)` is *assignment, not an equality check. See [In javascript == vs =?](https://stackoverflow.com/q/11871616) – VLAZ May 04 '21 at 16:10
  • @VLAZ Yeah that was a typo, but I still don't know how to fix this... – TheClassyCoder May 04 '21 at 16:13
  • @Dorothy did you look at the first link I posted which is about stopping intervals? – VLAZ May 04 '21 at 16:14
  • @VLAZ Yes, I'm trying to use that way, the difference is that I have an arrow function inside `doSomething` that I can't seem to handle it. – TheClassyCoder May 04 '21 at 16:16
  • @VLAZ Do you know a solution to my problem? The answer that dude just posted doesn't appeal for the `doSomething` to execute after some time. – TheClassyCoder May 04 '21 at 16:37

1 Answers1

1

You can call clearInterval inside the setInterval - I think this is what you're trying to achieve:

let intervalId;

function doSomething() {
  let a = 1;

  return setInterval(() => {
    console.log(a);

    if (a++ === 6) {
      clearInterval(intervalId);
    }
  }, 1000);
}

setTimeout(() => {
  intervalId = doSomething();
}, 5000);

console.log('Waiting for 5 seconds before calling doSomething..');
Tom O.
  • 5,730
  • 2
  • 21
  • 35
  • Thsnks, but how can I make `doSomething` execute after some time? – TheClassyCoder May 04 '21 at 16:24
  • Thanks but how can I make the `doSomething` to execute after some time? Using `setTimeout(doSomething, 5000)` seems to make clearInterval not to work. – TheClassyCoder May 04 '21 at 16:26
  • @Dorothy I've update the code example to address your comment that you want it to wait for some time before calling the `doSomething` – Tom O. May 04 '21 at 16:46