Why isn't setTimeout cancelling my loop?
问题 I wondered how many times can a JavaScript while statement (in Chrome's console) can increment a variable in a millisecond, so I quickly wrote this snippet directly into console: var run = true, i = 0; setTimeout(function(){ run = false; }, 1); while(run){ i++; } The problem is that it runs forever. Why is this happening, and how can I solve it? 回答1: This comes back to the one-threaded nature of JavaScript 1 . What happens is pretty much this: Your variables are assigned. You schedule a