-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsettimeout&setInterval.html
More file actions
179 lines (174 loc) · 6.21 KB
/
settimeout&setInterval.html
File metadata and controls
179 lines (174 loc) · 6.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>settimeout&&setinterval</title>
</head>
<body>
</body>
<script>
// setTimeout 主要是间隔一段时间执行逻辑,setInterval 主要是每隔相同的时间执行逻辑,众所周知浏览器中是是有event-loop的但是在这二者均属于宏任务,所以被触发的时候是需要查看任务队列清空的时候才有机会获得执行时机,那么问题出现了,setInterval 在执行的时候会在间隔一段时间内防止在任务队列,但是会统一清空也就是说会有可能 setInterval 会一次性执行多个不会有间隔,这就是为什么不建议使用 setInterval 而采用 setTimeOut 代替 setInterval,因为多个 setTimeout 放进去以后就会按照一定的间隔去执行多个,这就是二者的差别。累计效应(上面提到的),如果setInterval代码在(setInterval)再次添加到队列之前还没有完成执行, 就会导致定时器代码连续运行好几次,而之间没有间隔。 就算正常间隔执行,多个setInterval的代码执行时间可能会比预期小(因为代码执行需要一定时间)
// setTimeout 中的 this 如果是普通函数指向的都是 window,setTimeout中所执行函数中的this,永远指向window!!注意是要延迟执行的函数中的this哦!!setTimeout 中的匿名函数指向是自身的绑定的 this,但是非匿名函数内部的 this 指向的是 window
var i = 90;
let o = {
i: 12,
fn: function() {
console.log(this.i, this);
setTimeout(function(){console.log(this)
// window
})
}
}
setTimeout(function(){o.fn()}, 0)
// 12
setTimeout(o.fn, 0)
// 90
// 一、setTimeout中的延迟执行代码中的this永远都指向window
// 二、setTimeout(this.method, time)这种形式中的this,即上文中提到的第一个this,认为全局作用域,但不一定总是处于全局下,具体问题具体分析。
// 三、setTimeout(匿名函数, time)这种形式下,匿名函数中的变量也需要根据上下文来判断,具体问题具体分析
// setInterval 也遵循这样的逻辑,所以需要注意和处理。
// 用setTimeout实现setInterval
function interval(delay, fn) {
let timer = null
function fns(...args) {
let self = this;
timer = setTimeout(function() {
fn.apply(self, args);
fns.apply(self, args)
}, delay)
return function() {
clearTimeout(timer)
}
}
return fns;
}
// {a:[{b:["c","d"]}]} => {"a[0].b[0]":"c","a[0].b[1]":"d"}
// 打平JSON对象
let all = {}
function flattenObj(obj, parent) {
if(obj instanceof Array) {
for(let i = 0; i < obj.length; i++) {
common(obj[i], `[${i}]`, parent)
}
} else {
for(let i in obj) {
common(obj[i], i, parent)
}
}
}
function common(v, key, parent) {
let i = ''
if(parent !== '' && key.indexOf('[') === -1) {
i = '.' + i
}
let p = parent + i + key
if(typeof v === 'object') {
flattenObj(v, p)
} else {
all[p] = v
}
}
flattenObj({
"a": {
"b": {
"c": {
"d": 1
}
}
},
"aa": 2,
"c": [
1,
2
]
}, '');
// 打平json对象和恢复json对象
let falttenJSONObj = (function(){
let result = {}
let f = {}
function flattenObj(obj, parent) {
if(obj instanceof Array) {
for(let i = 0; i < obj.length; i++) {
common(obj[i], `[${i}]`, parent)
}
} else {
for(let i in obj) {
common(obj[i], i, parent)
}
}
}
function common(v, key, parent) {
let i = ''
if(parent !== '' && key.indexOf('[') === -1) {
i = '.' + i
}
let p = parent + i + key
if(typeof v === 'object') {
flattenObj(v, p)
} else {
result[p] = v
}
}
function flattenJSON(obj) {
result = {};
flattenObj(obj, '');
return result;
}
function ci(key, parent, v) {
if(key.indexOf('[') > -1) {
let p;
if(!parent[key.split('[')[0]]) {
parent[key.split('[')[0]] = [];
}
p = parent[key.split('[')[0]]
p[parseInt(key.split('[')[1],10)] = v || {};
return p
}else {
parent[key] = v || parent[key] || {};
parent = parent[key];
return parent
}
}
function flattenJSONToObj(obj) {
f = {};
let parent = f;
for(let i in obj) {
let key = (i+'').split('.');
if(key.length === 1) {
ci(key[0], f, obj[i])
} else {
parent = ci(key[0], f)
for(let j = 1; j < key.length; j++) {
if(j === key.length - 1) {
parent = ci(key[j], parent, obj[i])
}else {
parent = ci(key[j], parent)
}
}
}
}
return f;
}
return {
flattenJSON: flattenJSON,
flattenJSON2Obj: flattenJSONToObj,
}
})()
falttenJSONObj.flattenJSON({
"a": {
"b": {
"c": {
"d": 1
}
}
},
"aa": 2,
"c": [
1,
2
]
});
falttenJSONObj.flattenJSON2Obj({"a[0].b[0]":"c","a[0].b[1]":"d"});
falttenJSONObj.flattenJSON2Obj({"a.b.c.d": 1, "aa": 2, "c[0]": 1, "c[1]": 2});
</script>
</html>