stephenpassero
stephenpassero

Reputation: 469

Is there any way to pause a for loop until a variable is true

Is there a way to pause a for loop until a variable is true? For example, if I had this code,

var flag = false; 
for(var i = 0; i < 15; i++){
    console.log(i);
}

and i was equal to 7, could I pause the loop until flag = true?

Thanks!

Upvotes: 1

Views: 1830

Answers (5)

David
David

Reputation: 7295

JavaScript is a single-thread event-based model: if you could pause the execution you couldn't evaluate your flag during that time.

Also, if the value of the flag depends on an user event, you want to wait for the event to fire and not for the variable to change.

If, like in your example, you know beforehand when you want to pause the loop, you should split it:

for(var i = 0; i <= 7; i++){
    console.log(i);
}

document.querySelector("button").onclick = function() {
  for(var i = 8; i < 15; i++){
      console.log(i);
  }
}

If you don't know it (because it is decided inside the for loop) you just need to ensure that the next time the loop starts in the point it stopped (e.g., by reusing the i variable).

for (var i = 0; i < 15; i++) {
  console.log(i);
  if (Math.random() < 0.2) { break; }
}

document.querySelector("button").onclick = function() {
  for (i++; i < 15; i++) {
    console.log(i);
  }
}

If the code inside the loop is long you can wrap it inside a function (thanks @tom):

function logIndex() {
  //some code
  console.log(i);
}

Actually, you can isolate all the logic to manage the loop and the user event in a single function that, recursively, creates a handler for the user event to call itself. It has also the advantage that you can stop the loop as many times as needed.

var button = document.querySelector("button");

function logIndexes(index) {
  for (index; index < 15; index++) {
    console.log(index);
    if (Math.random() <= 0.2) {
      return button.onclick = function() {
        logIndexes(index + 1);
      }
    }
  }
  button.onclick = null;
}

logIndexes(0);
<button>Click me!</button>

Upvotes: 6

colecmc
colecmc

Reputation: 3318

ES6 has something called generators which may be helpful to you. It's not a for loop but could "pause" your script. Read more here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Iterators_and_Generators

function* idMaker() {
  let index = 0;
  while (index < 15) {
    yield index++;
  }
}

var gen = idMaker();

//console.log(gen.next().value);
//console.log(gen.next().value);

for (i of gen) {
  if (gen.next().done) {
    console.log('all done');
  } else {
    console.log(i);
  }
}

Upvotes: 0

Mohammad Sabri
Mohammad Sabri

Reputation: 26

Try this

var flag = false; 
for(var i = 0; i < 15; i++){ 
    If ((i > 6) && (! flag)){
        i = 6;
    } else {
         console.log(i); 
    }
}

This will made an infinite loop, but I think you know how & where flag will be true, may be the syntax is not true, but I give you the idea of how to pause a loop...

Upvotes: 0

Nina Scholz
Nina Scholz

Reputation: 386680

You could use an interval an a timeout for simulating a loop and for changing the flag.

function count(start, end, inc) {
    var i = start;
    return function () {
        if (i === 8 && ! flag) {
            return;        
        }
        console.log(i);
        i += inc;        
        if (i >= end) {
            clearInterval(interval);
        }
    }
}


var flag = false,
    interval = setInterval(count(0, 10, 1), 500);

setTimeout(function () {
    flag = true;
}, 10000);
.as-console-wrapper { max-height: 100% !important; top: 0; }

Upvotes: 1

DariusFontaine
DariusFontaine

Reputation: 794

No. A loop will execute until a condition is true.

Upvotes: 0

Related Questions