我在for循环中调用setTimeout时发生了什么?

时间:2017-02-15 07:07:47

标签: javascript

如您所见,请运行代码段,控制台输出如下:

CREATE TABLE `antest` (
  `i` int(10) unsigned NOT NULL,
  `c` char(80) default NULL,
  KEY `i` (`i`),
  KEY `c` (`c`,`i`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1

mysql> select count(distinct c) from antest;
+-------------------+
| count(distinct c) |
+-------------------+
|               101 |
+-------------------+
1 row in set (0.36 sec)


mysql> select count(distinct i) from antest;
+-------------------+
| count(distinct i) |
+-------------------+
|               101 |
+-------------------+
1 row in set (0.20 sec)

mysql> select count(distinct i,c) from antest;
+---------------------+
| count(distinct i,c) |
+---------------------+
|               10201 |
+---------------------+
1 row in set (0.43 sec)

mysql> show index from antest;
+--------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+
| Table  | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment |
+--------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+
| antest |          1 | i        |            1 | i           | A         |        NULL |     NULL | NULL   |      | BTREE      |         |
| antest |          1 | c        |            1 | c           | A         |        NULL |     NULL | NULL   | YES  | BTREE      |         |
| antest |          1 | c        |            2 | i           | A         |        NULL |     NULL | NULL   |      | BTREE      |         |
+--------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+
3 rows in set (0.00 sec)

mysql> analyze table sys_users;
+--------------------------------+---------+----------+----------+
| Table                          | Op      | Msg_type | Msg_text |
+--------------------------------+---------+----------+----------+
| antest                         | analyze | status   | OK       |
+--------------------------------+---------+----------+----------+
1 row in set (0.01 sec)


mysql> show index from antest;
+--------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+
| Table  | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment |
+--------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+
| antest |          1 | i        |            1 | i           | A         |         101 |     NULL | NULL   |      | BTREE      |         |
| antest |          1 | c        |            1 | c           | A         |         101 |     NULL | NULL   | YES  | BTREE      |         |
| antest |          1 | c        |            2 | i           | A         |       10240 |     NULL | NULL   |      | BTREE      |         |
+--------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+
3 rows in set (0.01 sec)

据我所知,function hello(a = ''){ if(a != '') alert("hello"); } 创建了一个新的函数堆栈,var hello = function(a = ''){ if(a != '') alert("hello"); } 应该在E/browser (26360): Console: Uncaught SyntaxError: Unexpected token =:122 时停止,所以我无法理解为什么1 ... 5 6 (5 times)

This is MDN about explain setTimeout.

从1到5我很容易理解,但是在调用setTimeout时发生了什么,以及如何在控制台中解释i<=5

&#13;
&#13;
i == 5
&#13;
&#13;
&#13;

2 个答案:

答案 0 :(得分:0)

第一个console.log调用在setTimeout中的调用开始之前连续运行五次。然后,i为6,console.log(i)被调用五次,这些都是排队的。

运行setTimeout时,它会返回计时器的句柄。您实际上并没有使用返回值执行任何操作,但它仍然在队列中。

此外,超时值0在技术上意味着&#34;现在运行&#34;,它只是意味着&#34;添加到队列的顶部&#34;。

答案 1 :(得分:0)

for(let i = 1; i <= 5; i++) {
    setTimeout(function() {
      console.log(i)
    }, 0)
}

var创建一个变量,该变量没有作用于for循环的块(它不是块作用域),而是这样做。因此,使用setTimeout调用创建的函数将全部查看相同的var,因此打印6(它的值在它们有机会运行时),而使用let,它们对此块作用域变量具有闭包,因此所有都正确打印他们的预期值,1..5。