setTimeout with Loop in JavaScript

后端 未结 9 1581
挽巷
挽巷 2021-01-05 16:08

I have a very trivial question. For a simple loop with setTimeout, like this:

for (var count = 0; count < 3; count++) {
    setTimeout(function() {
               


        
9条回答
  •  孤城傲影
    2021-01-05 16:31

    Think about it:

    1. The code executes a loop, in that loop it sets some code to run later.
    2. The loop finishes.
    3. The setTimeout code executes. What's the value of count going to be? The loop finished ages ago...

提交回复
热议问题