setTimeout的工作原理
2016-03-22 08:56
183 查看
<span style="font-family:Courier New;color:#a9b7c6;"><span style="font-size: 10.5pt; background-color: rgb(43, 43, 43);"><script> var x = 1; setTimeout(function(){ x = 2; console.log("setTimeout----------"+x); },0); console.log("outer-----------"+x); </script>
</span></span>
</pre><pre name="code" class="html">输出如下图:
<img src="http://img.blog.csdn.net/20160322090139965?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast" style="font-family: 'Courier New'; font-size: 10.5pt;" alt="" />
原因是:setTimeout在线程空闲的时候才开始执行。
设置为0毫秒后执行,即系统空闲之后,隔0毫秒将函数加入执行队列。
因此在outer的输出之后系统空闲,输出setTimeout内的log。
相关文章推荐
- 多线程中的join(),yield()与优先级Priority
- 1、面向对象上课笔记。。。
- Junit使用教程(二)
- UIView之setNeedsDisplay与drawRect 和 setNeedsLayout与layoutSubViews
- 康托展开总结
- linux下的压缩和解压缩的命令总结
- 康托展开总结
- HTML制作邮箱
- 微服务(Micro Service Architecture)浅析
- Junit使用教程(一)
- U盘掉了
- SAP主数据字段前导零 策略
- POJ 1286 解题报告
- Android实现控件随手指滑动缩放
- 进阶级-正则表达式
- 【HTML5】H5的新标签简介
- 20160322 股票入门
- 倍增法lca
- CLUSTER集群与存储技术
- Java中动态地改变数组长度及数组转Map的代码实例分享