About setTimeout Asynchronous queues

topic description

Why do you use let to declare variables to achieve the effect of outputting "0, 1, 2, 3"
mark

Mar.29,2021

background knowledge. Scope involving let and var

    The scope of
  1. let is at the block level. Intuitively, within {}, the variable of curly braces is invalid.
  2. The scope of
  3. var is within the function that defines this variable, and if it is not within the function, it is global.

background knowledge 2. SetTimeout is executed asynchronously, and it will not be executed until the Synchronize code is complete. In this case, the function in each setTimeout will not be executed until the for loop has finished running.

process deduction:

  1. each of the two for loops creates four setTimeout, both of which are passed in the variable I, waiting for execution to begin after the end of the for loop.
  2. after the end of the for cycle, all the four I passed in the 1:var-for based on background knowledge are re-assigned until the last time, the assignment is 4.
    the four I passed in the let-for do not affect each other within {}, and the post-order cycle does not reassign the I in the previous cycle (because each cycle is in an independent {} scope), so it can output the expected value normally
  3. .
MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1e51d8a-596ee.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1e51d8a-596ee.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?