#How do I make it stop skipping loops?

5 messages · Page 1 of 1 (latest)

stable inlet
#
function test() {
  let counter = 0;
  var bool = false;
  const i = setInterval(function(){
      console.log(counter);
      counter++;
      if(counter === 5) {
        bool = true;
        clearInterval(i);
      }
  }, 200);
  return bool;
}
if (test() == true) {
  console.log(true);
} else {
  console.log(false);
}

Output

false
0
1
2
3
4
What i want it to do
0
1
2
3
4
true

plain coral
#

The test function doesn't wait for setInterval to finish, do bool is never set to true by your interval function.

To do what you want to do you'd need to use a promise or have a function that you call when the interval is cancelled.

stable inlet
#

I'll try to use promise, im new to js just saying

plain coral
#

You'd need to call resolve when you clear the interval