setTimeout with Loop in JavaScript

后端 未结 9 1697
挽巷
挽巷 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:14

    That's because all the timeouts are run when the loop finished.

    The timeout functions then take the current value of count.

    And thats always 3 because the for loop has finished.

提交回复
热议问题