1

I have a piece of code with while loop which I would like to stop by setTimeout(). But it seems like a endless loop, which never triggers setTimeout(). If I remove while loop, timeout triggers correctly. What is wrong please?

$(document).ready(function() 
{
    var i = 0, s = false;

    setTimeout( function()
    {
        s = true;
        console.log( "Timeuot!!!" );
        console.log( "s value is " + s );
    }, 1000 );

    while( s === false )
    {
        console.log( "this is while and s is " + s );
        i++;
    }

    console.log( "iterations: " + i );
});
4
  • @Quentin the s inside the setTimeout is set at the global scope. why am I wrong ? Commented Oct 13, 2016 at 11:46
  • @RoyiNamir — The function passed to setTimeout is defined inside another function. It has access to the scope of that function. Since there is a non-global s in that scope, it accesses that s. Commented Oct 13, 2016 at 11:47
  • @RoyiNamir h s inside the setTimeout is the one of the enclosing scope so the one declared and defined a couple of lines above. Commented Oct 13, 2016 at 11:48
  • @Quentin oh man you're right. my bad. deleting. Commented Oct 13, 2016 at 11:49

3 Answers 3

7

JavaScript runs a single event loop. It won't stop in the middle of a function to see if there are any events (such as clicks or timeouts) that would trigger a different function.

In short: It won't run the timed function until the while loop has finished.


To do this sort of thing, you'd normally have an event driven iterator.

var i = 0,
  s = false;

setTimeout(function() {
  s = true;
  console.log("Timeuot!!!");
  console.log("s value is " + s);
}, 1000);

next();

function next() {
  if (s) {
    return done();
  }
  console.log({
    s, i
  });
  i++;
  setTimeout(next, 0);
}

function done() {
  console.log("iterations: " + i);
}

Sign up to request clarification or add additional context in comments.

2 Comments

Mmmm. But it does stop and shows that it is not infinite loop .jsbin.com/cewewarovo/1/edit?html,js,console,output
@RoyiNamir — JSbin appears to mangle code to stop people doing DOS attacks with it. while (s === false) {if (window.runnerWindow.protect.protect({line: 13}))break;window.runnerWindow.proxyConsole.log("this is while and s is " + s);i++;} — It adds a break in there.
1

As already mentioned the while loop blocks the one and only thread. To let your example do the thing you want, replace the while loop with setInterval(function) like this:

$(document).ready(function() 
{
    var i = 0, s = false;

    setTimeout( function()
    {
        s = true;
        console.log( "Timeout!!!" );
        console.log( "s value is " + s );
    }, 1000 );


    var interval = setInterval(function() {
      console.log( "this is while and s is " + s );
      i++;      
      if (s) {
        clearInterval(interval);
        console.log("i is " + i)
      }
    }, 100);    
});

Comments

0

setTimeout is never called do the the fact that the while never ends and so the even dispatcher is not going to trigger the setTimeout.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.