JavaScript:异步 setTimeout
setTimeout() 方法用于在指定的毫秒数后调用函数或计算表达式。
function showDate(){
var date=new Date();
console.log(date);
}
console.log("aa");
showDate();
console.log("bb");
setTimeout(function(){showDate();},);
console.log("cc");
showDate();

可以发现setTimeout内部的function是等待10s以后再执行的。
把时间改为0

发现它还是最后执行的
类似异步
更具体的,更详细的
Date.prototype.format = function(formatStr)
{
var str = formatStr;
var Week = ['日','一','二','三','四','五','六']; str=str.replace(/yyyy|YYYY/,this.getFullYear());
str=str.replace(/yy|YY/,(this.getYear() % )>?(this.getYear() % ).toString():'' + (this.getYear() % ));
//
this.setMonth(this.getMonth()+);
str=str.replace(/MM/,this.getMonth()>?this.getMonth().toString():'' + this.getMonth());
str=str.replace(/M/g,this.getMonth()); str=str.replace(/w|W/g,Week[this.getDay()]); str=str.replace(/dd|DD/,this.getDate()>?this.getDate().toString():'' + this.getDate());
str=str.replace(/d|D/g,this.getDate()); str=str.replace(/hh|HH/,this.getHours()>?this.getHours().toString():'' + this.getHours());
str=str.replace(/h|H/g,this.getHours());
str=str.replace(/mm/,this.getMinutes()>?this.getMinutes().toString():'' + this.getMinutes());
str=str.replace(/m/g,this.getMinutes()); str=str.replace(/ss|SS/,this.getSeconds()>?this.getSeconds().toString():'' + this.getSeconds());
str=str.replace(/s|S/g,this.getSeconds()); str=str.replace(/ffff/,this.getMilliseconds()); return str;
} function showDate(){
var date=new Date();
var str=date.format("yyyy-MM-dd HH:mm:ss:ffff");
console.log(str);
}
console.log("aa");
showDate();
console.log("bb");
setTimeout(function(){showDate();},);
console.log("cc");
showDate();

时间为100微秒

差距都差不多,更setTimeout间隔时间无关.
那么setTimeout是不是就是运行到setTimeout时就在任务队列中添加一个方法,setTimeout时间到了就运行该方法?
其实发现不是
有特殊情况:
假如setTimeout的间隔时间小于任务队列后面运行的时间:
function showDate(){
var date=new Date();
var str=date.format("yyyy-MM-dd HH:mm:ss:ffff");
console.log(str);
}
console.log("aa");
showDate();
console.log("bb");
setTimeout(function(){console.log("bbb");showDate();},);
console.log("cc");
showDate();
console.log("begin");
for(var i=;i<;i++){
}
console.log("end");
showDate();

发现:setTimeout的间隔时间是近5.5s,for循环花费的时间也大概是5.5s
说明:在这种setTimeout间隔时间小于任务队列运行时间的情况下,setTimeout中的方法一直到队列任务全部完成以后才执行,间隔时间有任务队列花费时间决定。
setTimeout间隔时间大于队列任务运行时间,实际任务中大部分是这种情况
function showDate(){
var date=new Date();
var str=date.format("yyyy-MM-dd HH:mm:ss:ffff");
console.log(str);
}
console.log("aa");
showDate();
console.log("bb");
setTimeout(function(){console.log("bbb");showDate();},);
console.log("cc");
showDate();
console.log("begin");
for(var i=;i<;i++){
}
console.log("end");
showDate();

发现尽管for循环中花费了5.5s,但是setTimeout的间隔时间依然是30s.
这时再回头看看那个setTimeout(function(){},0),间隔时间是0的情况
发现setTimeout中的方法就应该在任务队列的最后运行。
证明:js引擎按照预编译的顺序运行队列任务,这时setTimeout中的方法没有在队列中,间隔时间到了以后将setTimeout中的方法添加到任务队列中
所以,setTimeout中的方法都是在预编译中的方法后运行的
多个setTimeout中的方法运行顺序是由它们加入队列的顺序决定的。
function showDate(){
var date=new Date();
var str=date.format("yyyy-MM-dd HH:mm:ss:f");
console.log(str);
}
console.log("aa");
showDate();
console.log("bb");
setTimeout(function(){console.log("bbb");showDate();},);
console.log("cc");
showDate();
console.log("begin");
for(var i=;i<;i++){
}
console.log("end");
showDate();
console.log("begin2");
for(var i=;i<;i++){
}
console.log("end2");
showDate();

继续分析:
1.两个setTimeout
function showDate(){
var date=new Date();
var str=date.format("yyyy-MM-dd HH:mm:ss:ffff");
console.log(str);
}
console.log("aa");
showDate();
console.log("bb");
setTimeout(function(){console.log("bbb");showDate();},);
console.log("cc");
setTimeout(function(){console.log("ccc");showDate();},);
showDate();

2.第一个setTimeout的方法花费时间大于第二个setTimeout的间隔时间:
function showDate(){
var date=new Date();
var str=date.format("yyyy-MM-dd HH:mm:ss:ffff");
console.log(str);
}
console.log("aa");
showDate();
console.log("bb");
setTimeout(function(){console.log("bbb");showDate();},);
console.log("cc");
setTimeout(function(){
console.log("ccc");
showDate();
console.log("begin");
for(var i=;i<;i++){
}
console.log("end");
showDate();
},);
showDate();

发现:for循环花费了6s多,bbb是紧接着前一个setTimeout方法的,
http://www.cnblogs.com/littledu/articles/2607211.html
http://www.laruence.com/2009/09/23/1089.html
http://www.cnblogs.com/rubylouvre/archive/2009/08/20/1550264.html
http://www.cnblogs.com/rubylouvre/archive/2011/03/14/1982699.html
JavaScript:异步 setTimeout的更多相关文章
- Javascript异步编程之setTimeout与setInterval详解分析(一)
Javascript异步编程之setTimeout与setInterval 在谈到异步编程时,本人最主要会从以下三个方面来总结异步编程(注意:特别解释:是总结,本人也是菜鸟,所以总结不好的,请各位大牛 ...
- 【转】Javascript异步编程之setTimeout与setInterval
Javascript异步编程之setTimeout与setInterval 转自:http://www.tuicool.com/articles/Ebueua 在谈到异步编程时,本人最主要会从以下三个 ...
- JavaScript异步编程的主要解决方案—对不起,我和你不在同一个频率上
众所周知(这也忒夸张了吧?),Javascript通过事件驱动机制,在单线程模型下,以异步的形式来实现非阻塞的IO操作.这种模式使得JavaScript在处理事务时非常高效,但这带来了很多问题,比如异 ...
- JavaScript异步编程原理
众所周知,JavaScript 的执行环境是单线程的,所谓的单线程就是一次只能完成一个任务,其任务的调度方式就是排队,这就和火车站洗手间门口的等待一样,前面的那个人没有搞定,你就只能站在后面排队等着. ...
- javascript异步编程的前世今生,从onclick到await/async
javascript与异步编程 为了避免资源管理等复杂性的问题, javascript被设计为单线程的语言,即使有了html5 worker,也不能直接访问dom. javascript 设计之初是为 ...
- JavaScript异步编程(2)- 先驱者:jsDeferred
JavaScript当前有众多实现异步编程的方式,最为耀眼的就是ECMAScript 6规范中的Promise对象,它来自于CommonJS小组的努力:Promise/A+规范. 研究javascri ...
- 【转】JavaScript 异步进化史
前言 JS 中最基础的异步调用方式是 callback,它将回调函数 callback 传给异步 API,由浏览器或 Node 在异步完成后,通知 JS 引擎调用 callback.对于简单的异步操作 ...
- 对Javascript异步执行的理解
简单的查看了下Javascript异步编程的代码.按照网上的说法,Javascript异步编程的核心就在于setTimeout.这个系统函数让我们将函数的执行放在了一个指定的新“线程”中.于是本来的顺 ...
- Promises与Javascript异步编程
Promises与Javascript异步编程 转载:http://www.zawaliang.com/2013/08/399.html 在如今都追求用户体验的时代,Ajax应用真的是无所不在.加上这 ...
随机推荐
- MVC Controller Dependency Injection for Beginners【翻译】
在codeproject看到一篇文章,群里的一个朋友要帮忙我翻译一下顺便贴出来,这篇文章适合新手,也算是对MEF的一个简单用法的介绍. Introduction In a simple stateme ...
- Angular2表格/可排序/table
Angular2表格 1. 官网下载Angular2开发环境,以及给出的quickstart代码示例demo(地址如下),具体步骤不在详述. https://github.com/angular/qu ...
- display:box和display:inline-box的区别
display:box我想大家很熟悉,那么display:inline-box呢,今天在项目中需要设置这样的属性box-align:center,那么就想到用 display:box;如果设置BOX, ...
- pyhon 模块与库
引用“http://www.iplaypython.com/module/” 引用“http://codingnow.cn/language/265.html” Python模块是什么? 玩蛇网:一个 ...
- java 反射实践
/** * * @author yuxg */ import coreJava.javaFile; import javaClassStudy.Student; import javaClassStu ...
- TodoMVC中的Backbone+MarionetteJS+RequireJS例子源码分析之二 数据处理
当我们使用jQuery时大部分时间是聚焦于Dom节点的处理,给Dom节点绑定事件等等:前端mvc框架backbone则如何呢? M-Model,Collection等,是聚焦于数据的处理,它把与后台数 ...
- [Android]关于filed 遍历资源文件的排序问题
Field[] svgfields = R.drawable.class.getFields(); listid = new ArrayList<Integer>(); for (Fiel ...
- Odoo 8.0 new API 之Environment
""" An environment wraps data for ORM records: - :attr:`cr`, the current database cur ...
- Python之路第一课Day3--随堂笔记(文件操作)
一.集合的介绍 1.集合操作 集合是一个无序的,不重复的数据组合,它的主要作用如下: 去重,把一个列表变成集合,就自动去重了 关系测试,测试两组数据之前的交集.差集.并集等关系 常用操作 s = se ...
- Areas on the Cross-Section Diagram
Areas on the Cross-Section Diagram Aizu - ALDS1_3_D Areas on the Cross-Section Diagram 地域の治水対策として.洪 ...