Skip to content

Commit 71e0107

Browse files
committed
定时器
1 parent af2bf00 commit 71e0107

3 files changed

Lines changed: 45 additions & 173 deletions

File tree

JS高级技巧/高级定时器.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
高级定时器
2+
3+
**关于定时器要记住的最重要的事情是**:指定的时间间隔表示何时将定时器的代码添加到队列,而不是何时实际执行代码。
4+
5+
> 定时器对队列的工作方式是,当特定时间过去后将代码插入。注意,给队列添加代码并不意味着对它立刻执行,而只能表示它会尽快执行。设定一个 150ms 后执行的定时器不代表到了 150ms代码就立刻执行,它表示代码会在 150ms 后被加入到队列中。如果在这个时间点上,队列中没有其他东西,那么这段代码就会被执行,表面上看上去好像代码就在精确指定的时间点上执行了。其他情况下,代码可能明显地等待更长时间才执行。
6+
7+
定时器仅仅只是计划代码在未来的某个时间执行。执行的时间是不能保证的,因为在页面的生命周期中,不同时间可能有其他代码在控制 JavaScript 进程。在页面下载完后的代码运行、事件处理程序、Ajax 回调函数都必须使用同样的线程来执行。实际上,浏览器负责进行排序,指派某段代码在某个时间点运行的优先级。
8+
9+
可以把 JavaScript 想象成在时间线上运行的。当页面载入时,首先执行是任何包含在 < script> 元素中的代码,通常是页面生命周期后面要用到的一些简单的函数和变量的声明,不过有时候也包含一些初始数据的处理。在这之后,JavaScript 进程将等待更多代码执行。当进程空闲的时候,下一个代码会被触发并立刻执行。例如,当点击某个按钮时, onclick 事件处理程序会立刻执行,只要 JavaScript 进程处于空闲状态。这样一个页面的时间线类似于下图(引自高程三)
10+
11+
![](http://i.imgur.com/rSFcbes.png)
12+
13+
下面是有循环的代码
14+
15+
<div id="test">2222222222</div>
16+
<script>
17+
var oTest=document.getElementById("test");
18+
oTest.onclick=function(){
19+
var nowTime=new Date();
20+
setTimeout(function(){
21+
console.log("定时器的代码");
22+
console.log(new Date()-nowTime);//534
23+
},100);
24+
for(var i=0;i<1000000000;i++){}
25+
var clickTime=new Date();
26+
console.log(clickTime-nowTime);//532
27+
}
28+
</script>
29+
30+
点击后,会发现,并不是过100毫秒执行的;如果去掉for循环,那是差不多的;

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,5 +114,8 @@
114114
- 高级函数
115115
- 防篡改对象
116116
- 高级定时器
117+
- [高级函数](./JS高级技巧/高级函数.md)
118+
- [防篡改对象](./JS高级技巧/防篡改对象.md)
119+
- [高级定时器](./JS高级技巧/高级定时器.md)
117120

118121
- 编写可维护的javascript代码,性能优化与部署

test-file.html

Lines changed: 12 additions & 173 deletions
Original file line numberDiff line numberDiff line change
@@ -1,184 +1,23 @@
11
<!doctype html>
2-
<html>
2+
<html lang="en">
33
<head>
44
<meta charset="UTF-8">
55
<title>Document</title>
6-
<style>
7-
html, body {
8-
width: 100%;
9-
height: 100%;
10-
position: relative;
11-
}
12-
13-
.diy-window {
14-
position: fixed;
15-
width: 500px;
16-
height: 150px;
17-
border: 1px dashed darkcyan;
18-
/*opacity: 0.2;*/
19-
}
20-
21-
.window-title {
22-
padding: 10px 15px;
23-
background-color: #f4f4f4;
24-
position: relative;
25-
}
26-
27-
.window-title .close-window {
28-
display: inline-block;
29-
position: absolute;
30-
right: 20px;
31-
cursor: pointer;
32-
}
33-
34-
.diy-window .window-body {
35-
position: absolute;
36-
right: 50px;
37-
bottom: 30px;
38-
}
39-
.test-one {
40-
width: 500px;
41-
height: 300px;
42-
background-color: #37C7D4;
43-
border-radius: 30px 10px;
44-
-webkit-border-radius: 30px 10px;
45-
}
46-
.test-two {
47-
width: 500px;
48-
height: 300px;
49-
background-color: #d46c4d;
50-
-webkit-border-radius: 30px 10px;
51-
border-radius: 30px 10px;
52-
}
53-
</style>
546
</head>
557
<body>
56-
57-
<div class="test-one">
58-
<ul>
59-
<li><a href=""></a></li>
60-
<li><a href=""></a></li>
61-
<li><a href=""></a></li>
62-
<li><a href=""></a></li>
63-
</ul>
64-
</div>
65-
66-
<div class="test-two">
67-
<ul>
68-
<li><a href=""></a></li>
69-
<li><a href=""></a></li>
70-
<li><a href=""></a></li>
71-
<li><a href=""></a></li>
72-
</ul>
73-
</div>
74-
<DIV>
75-
<UL>
76-
<LI><A></A></LI>
77-
<LI><A></A></LI>
78-
<LI><A></A></LI>
79-
<LI><A>
80-
</A></LI>
81-
</UL>
82-
</DIV>
83-
84-
85-
<button class="wui-button wui-button-s wui-button-orange j-block-today-user"
86-
data-users="{&quot;data&quot;:[{&quot;user_id&quot;:&quot;723579806832907230&quot;,&quot;wx_nick_name&quot;:&quot;暖宝宝&quot;,&quot;black_status&quot;:2,&quot;wx_head_img_url&quot;:&quot;http://wx.qlogo.cn/mmopen/qGusKyb0IEchePqdeTeVSkgNy7TKehZCFlK5XSjxlt7rGLoaGeqdPnHOtI28gvn8ZseicboUDwFprYVIico8SVxQ/0&quot;},{&quot;user_id&quot;:&quot;721899587060764672&quot;,&quot;wx_nick_name&quot;:&quot;名字还没想好。。&quot;,&quot;black_status&quot;:2,&quot;wx_head_img_url&quot;:&quot;http://wx.qlogo.cn/mmopen/Mb2TiaPnTNictgZU0DHJwfbCHlxg3niaaIgibztSYm0S2oFP3nw1Ny1iaB4gPPykIQUyyVEWSB0GjIpCeIZaJXTZWib3hPJWkqiadut/0&quot;},{&quot;user_id&quot;:&quot;721524177898700800&quot;,&quot;wx_nick_name&quot;:&quot;小海螺 ~&quot;,&quot;black_status&quot;:2,&quot;wx_head_img_url&quot;:&quot;http://wx.qlogo.cn/mmopen/ajNVdqHZLLBa8boOZ7cNjY9uYPico3VC2u2nvcaSqTibaibG0B8YJoMFeekfVlnPmpOahcH1e4RNPposaRQice2jMg/0&quot;}],&quot;count&quot;:3}">
87-
<i class="iconfont wui-txt-sm"></i> 拉黑该IMEI用户
88-
</button>
89-
90-
<div id="dsdsdsd"></div>
91-
<mark>Text</mark>
92-
<strong>Text</strong>
93-
<em>Text</em>
94-
95-
<span id="test">ddddd</span>
96-
97-
<div class="diy-window">
98-
<div class="window-title">
99-
<span>的感觉如何</span>
100-
<span class="close-window" id="close-window">X</span>
101-
</div>
102-
<div class="window-body">
103-
<input type="button" class="select-btn" id="input-one" name="islike" value="我喜欢"/>
104-
<input type="button" class="select-btn" id="input-two" name="islike" value="不喜欢"/>
105-
</div>
106-
</div>
107-
108-
109-
<br/>
110-
<br/>
111-
<br/>
112-
<br/>
113-
<br/>
114-
<br/>
115-
<br/>
116-
<br/>
117-
<br/>
118-
<br/>
119-
<br/>
120-
121-
<a target="_blank" href="http://mail.qq.com/cgi-bin/qm_share?t=qm_mailme&[email protected]" style="text-decoration:none;"><img src="http://rescdn.qqmail.com/zh_CN/htmledition/images/function/qm_open/ico_mailme_02.png"/></a>
122-
123-
<br/>
124-
125-
<div class="shake-monery-user">
126-
<span class="shake-monery-user-avatar">
127-
<img src="https://ss0.bdstatic.com/5aV1bjqh_Q23odCf/static/superman/img/logo_top_ca79a146.png" alt="用户头像">
128-
</span>
129-
<a href="/shakemoney/user_detail?id=688189193821626368" target="_blank" data-userid="688189193821626368" class="j-user-cardinfo">用户名字</a>
130-
</div>
131-
8+
<div id="test">2222222222</div>
1329
<script>
133-
var ooTest = document.getElementById("test");
134-
console.dir(ooTest);
135-
//todo 获取需要的元素
136-
var inoputOne = document.getElementById("input-one");
137-
var inoputTwo = document.getElementById("input-two");
138-
var closeWindow = document.getElementById("close-window");
139-
140-
//todo input-two的mouse事件
141-
inoputTwo.onmouseover = function () {
142-
inoputTwo.value = "我喜欢";
143-
inoputOne.value = "不喜欢";
144-
};
145-
inoputTwo.onmouseout = function () {
146-
defaultValue();
147-
};
148-
149-
inoputOne.onmouseover = function () {
150-
defaultValue()
151-
};
152-
inoputOne.onmouseout = function () {
153-
defaultValue();
154-
};
155-
156-
//todo 点击关闭
157-
closeWindow.onclick = function () {
158-
159-
};
160-
161-
//默认的value值
162-
function defaultValue() {
163-
inoputOne.value = "我喜欢";
164-
inoputTwo.value = "不喜欢";
10+
var oTest=document.getElementById("test");
11+
oTest.onclick=function(){
12+
var nowTime=new Date();
13+
setTimeout(function(){
14+
console.log("定时器的代码");
15+
console.log(new Date()-nowTime);//534
16+
},100);
17+
// for(var i=0;i<1000000000;i++){}
18+
var clickTime=new Date();
19+
console.log(clickTime-nowTime);//532
16520
}
166-
167-
"use strict";
168-
169-
var aaa=2;
170-
console.log(typeof aaa);//number
171-
aaa="222";
172-
console.log(typeof aaa);//string
173-
aaa=true;
174-
console.log(typeof aaa);//boolean
175-
aaa=null;
176-
console.log(typeof aaa);//object
177-
aaa=function(){};
178-
console.log(typeof aaa);//function
179-
aaa={"name":"test"};
180-
console.log(typeof aaa);//object
181-
18221
</script>
18322
</body>
18423
</html>

0 commit comments

Comments
 (0)