1

Can anybody explain to me why the following code is producing an infinite loop?

var car = 'Ford';       
var done = false;

setTimeout(function() {     
    car = 'Toyota';
    done = true;
}, 500);

while (!done) {}
alert(car);


Update: Answered here as well.

Community
  • 1
  • 1
Drazen Bjelovuk
  • 5,201
  • 5
  • 37
  • 64
  • 1
    The duplicate question should provide you with an answer, but why are you doing this anyway? Surely you just want to get rid of the `while` loop and move the `alert` into the `setTimeout` callback function – musefan Nov 14 '13 at 16:37
  • My actual code has something similar but encased inside of a function, and the alert is meant to be a return statement. I just tried simplifying it to serve as a clearer example. – Drazen Bjelovuk Nov 14 '13 at 16:43

0 Answers0