0

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;
}


This is another way I tried too:

     var counter = 0;
    var i = setInterval(function(){
       
       
    var a=Math.random();
    var b=Math.random();
    console.log("a:",a);
    console.log("b:",b);
       
        counter++;
        if(a<b || a=b) {
            clearInterval(i);
        }
    }, 200);
     

Please, any suggestions?
6
  • The timeout function doesn't run until after the loop finishes. Commented Oct 21, 2020 at 1:08
  • JavaScript is single-threaded, the timeout function can't run at the same time as the mainline code. Commented Oct 21, 2020 at 1:09
  • Also, every time through the loop you start another timeout function without cancelling the old one. You'll soon have thousands of timeout functions scheduled. Commented Oct 21, 2020 at 1:12
  • what is the solution? use setinterval? and here explaining what I am trying to do. I have a and b variables, those two var get updated every 3 seconds, so their values are changing constantly. I want to run a loop as long as the condition of a>b is exist. Once this condition is no longer exist, I want to break the loop and move one to next function Commented Oct 21, 2020 at 2:14
  • JavaScript is event-based. You don't run loops, because that blocks all interactions. If you want to do something periodically, use setInterval, not a loop. Commented Oct 21, 2020 at 3:05

1 Answer 1

0

Edit: This is more along the lines of what you want to accomplish. In your original case you had two problems A) The variables that you are checking are not in the correct function scopes and B) setTimeout functions get pushed into an event loop in javascript. Javascript will only run functions pushed into an event loop when the main thread is clear since javascript is single threaded. However because you have a while loop, that while loop will continue to run until it hits the break condition, never allowing your timeout function to be run, so it the while will run forever and the timeout function will never get run. This solution wraps the whole thing in a setInterval instead, setInterval will run periodically and then in this case is will clear itself when your condition is met.

var number = 0;
var interval;

function clear() {
    if (interval !== undefined) {
        clearInterval(interval);
    }
    callFunctionYouWantToMoveTo();
}

interval = setInterval(function() {
    console.log("Anything you want to print");  
    a=Math.random();
    b=Math.random();

    number++;

    if (a>b) return;

    clear();
}, 1000)
Sign up to request clarification or add additional context in comments.

4 Comments

This still won't work because the timeout function runs asynchronously, and won't run until the loop terminates.
Yeah the while loop will lock the thread so it will still either crash or break immediatly, (in this case it will break immediately since undefined > undefined is false). I think what he probably wants to do is wrap all this logic in a setInterval and then clear the setInterval on success;
What I am trying to do here, I have a and b variables, those two var get updated every 3 seconds, so their values are changing constantly. I want to run a loop as long as the condition of a>b is exist. Once this condition is no longer exist, I want to break the loop and move one to next function.
I have updated my original comment with something that should accomplish what you want.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.