mardi 20 octobre 2020

Combine settimeout and label Loop and if statement [duplicate]

The following code is work as the following

while loop>> bring a and b values>> settimeout>> check the value by if-condition>> continue it true, else break;

This code with settimeout and is not working:

var number = 0;

start_position: while (true) {
  setTimeout(function() {
    console.log("Anything you want to print");
    var a = Math.random();
    var b = Math.random();
  }, 1000)

  number++;

  if (a > b) continue start_position;
  break;
}

This code without settimeout and is work just fine:

var number = 0;

start_position: while (true) {
  console.log("Anything you want to print");
  var a = Math.random();
  var b = Math.random();
  number++;

  if (a > b) continue start_position;
  break;
}

Please, any suggestions?

Aucun commentaire:

Enregistrer un commentaire