JavaScript setTimeout in a for loop, variable not changing javascript settimeout loop variable

I'm a bit of a beginner to JavaScript and I've been trying to figure this out for at least two hours. If someone could explain to me why this is happening, it'll be great!

function slowDouble(x, callback) {
    setTimeout(function() {
    callback(2 * x);
  }, 500);
}

function slowDoubleTenTimes(x, callback) {
    for (i = 0; i < 10; i++) {
        slowDouble(x, function(result) {
            x = result;
        });
    }

    callback(x);
}

slowDoubleTenTimes(3, function(result){
  console.log('The result of slowDoubleTenTimes is ' + result);
});

Logic is telling me that in slowDoubleTenTimes, in the for loop, x should be changing. And every time it calls slowDouble again in the subsequent for-loop iteration, x should be different. But x remains at 3! In fact, the resulting answer in callback(x) should be 3072. Yet, x changes from 3 to 6 and then remains at 6.

Is there something about JavaScript that I don't know that's preventing the result from changing?

Also, the weird thing is, if I put console.log("hi") after the for-loop, the console prints out "hi" before slowDouble runs. Shouldn't slowDouble run before console.log("hi")? Or is there something about setTimeout that I'm not understanding correctly?

Thank you!

Answer:1

slowDouble does not block. So, "callback" is invoked right away.

setTimeout says, "run this function in 500 milliseconds". But, it doesn't block, which means that it continues to the next lines after it registered to be invoked in 500 ms.

Answer:2
  • JavaScript engines only have a single thread, forcing asynchronous events to queue waiting for execution.

  • If a timer is blocked from immediately executing it will be delayed
    until the next possible point of execution (which will be longer than the desired delay).

http://ejohn.org/blog/how-javascript-timers-work/

function slowDouble(x, callback) {
    setTimeout(function() {
    callback(2 * x);
  }, 500);
}

function slowDoubleTenTimes(x, callback) {
    for (i = 0; i < 10; i++) {
        slowDouble(x, function(result) {
            x = result;
        });
    }

    callback(x);  //This will get called before the callback from timeout
                  // in slowDouble's context is called. 
                  // console.log(3)

}

slowDoubleTenTimes(3, function(result){
  console.log('The result of slowDoubleTenTimes is ' + result);
});
Answer:3

I have the following code: //Marks all users which are reading the book with the bookId var markAsReading = function (bookId,cb) { User.find({}, function (err,users) { if(err) ...

I have the following code: //Marks all users which are reading the book with the bookId var markAsReading = function (bookId,cb) { User.find({}, function (err,users) { if(err) ...

  1. nodejs loop wait
  2. nodejs loop wait for callback
  3. nodejs while loop wait
  4. node js for loop wait for promise

In my web app, running in an android WebView, whenever I click on something, or navigate somewhere, that container div is highlighted blue. Sometimes only for a moment, but sometimes until you click ...

In my web app, running in an android WebView, whenever I click on something, or navigate somewhere, that container div is highlighted blue. Sometimes only for a moment, but sometimes until you click ...

  1. android highlight webview
  2. android webview highlight text
  3. android webview highlight search text
  4. android webview highlight selected text
  5. android webview highlight color

I'm just wondering whether it is at all possible to transfer a directory from a unix server to my local machine using the ssh2 module in node.js. I have connected to the remote host and can read the ...

I'm just wondering whether it is at all possible to transfer a directory from a unix server to my local machine using the ssh2 module in node.js. I have connected to the remote host and can read the ...

  1. transfer entire directory scp
  2. transfer whole directory
  3. ftp transfer entire directory
  4. ftp command line transfer entire directory
  5. ftp command to transfer entire directory

Well, I have some function that invokes via ng-click. For example, this function set variable a to true, and after few seconds variable should become false. Function looks like this one: $scope.do =...

Well, I have some function that invokes via ng-click. For example, this function set variable a to true, and after few seconds variable should become false. Function looks like this one: $scope.do =...

  1. make word clock
  2. make right click work on mac
  3. how to make clickshare work
  4. how to make clickbank work
  5. how to make clickshare work on mac
  6. make mouse double click work