/
防抖debounce与节流throttle.html
172 lines (163 loc) · 6.86 KB
/
防抖debounce与节流throttle.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>防抖debounce 与 节流throttle</title>
</head>
<body style="padding: 100px 20% 0 0; text-align: right">
<div>原生态:<input id="noAction" /></div>
<br />
<div>防抖100ms:<input id="debounce" /></div>
<br />
<div style="height: 100px">滑动节流100ms测试</div>
<div style="height: 100px">滑动节流100ms测试</div>
<div style="height: 100px">滑动节流100ms测试</div>
<div style="height: 100px">滑动节流100ms测试</div>
<div style="height: 100px">滑动节流100ms测试</div>
<div style="height: 100px">滑动节流100ms测试</div>
<div style="height: 100px">滑动节流100ms测试</div>
<div style="height: 100px">滑动节流100ms测试</div>
<div style="height: 100px">滑动节流100ms测试</div>
<div style="height: 100px">滑动节流100ms测试</div>
<div style="height: 100px">滑动节流100ms测试</div>
<div style="height: 100px">滑动节流100ms测试</div>
<div style="height: 100px">滑动节流100ms测试</div>
<div style="height: 100px">滑动节流100ms测试</div>
<div style="height: 100px">滑动节流100ms测试</div>
<div style="height: 100px">滑动节流100ms测试</div>
<div style="height: 100px">滑动节流100ms测试</div>
<div style="height: 100px">滑动节流100ms测试</div>
<div style="height: 100px">滑动节流100ms测试</div>
<div style="height: 100px">滑动节流100ms测试</div>
<div style="height: 100px">滑动节流100ms测试</div>
<div style="height: 100px">滑动节流100ms测试</div>
<div style="height: 100px">滑动节流100ms测试</div>
<div style="height: 100px">滑动节流100ms测试</div>
<div style="height: 100px">滑动节流100ms测试</div>
<div style="height: 100px">滑动节流100ms测试</div>
<div style="height: 100px">滑动节流100ms测试</div>
<div style="height: 100px">滑动节流100ms测试</div>
<div style="height: 100px">滑动节流100ms测试</div>
<div style="height: 100px">滑动节流100ms测试</div>
<div style="height: 100px">滑动节流100ms测试</div>
<div style="height: 100px">滑动节流100ms测试</div>
<div style="height: 100px">滑动节流100ms测试</div>
<div style="height: 100px">滑动节流100ms测试</div>
<div style="height: 100px">滑动节流100ms测试</div>
<div style="height: 100px">滑动节流100ms测试</div>
<div style="height: 100px">滑动节流100ms测试</div>
<div style="height: 100px">滑动节流100ms测试</div>
<div style="height: 100px">滑动节流100ms测试</div>
<div style="height: 100px">滑动节流100ms测试</div>
<div style="height: 100px">滑动节流100ms测试</div>
<div style="height: 100px">滑动节流100ms测试</div>
<div style="height: 100px">滑动节流100ms测试</div>
<div style="height: 100px">滑动节流100ms测试</div>
<div style="height: 100px">滑动节流100ms测试</div>
<div style="height: 100px">滑动节流100ms测试</div>
<div style="height: 100px">滑动节流100ms测试</div>
<div style="height: 100px">滑动节流100ms测试</div>
<div style="height: 100px">滑动节流100ms测试</div>
<div style="height: 100px">滑动节流100ms测试</div>
<div style="height: 100px">滑动节流100ms测试</div>
<div style="height: 100px">滑动节流100ms测试</div>
<div style="height: 100px">滑动节流100ms测试</div>
<div style="height: 100px">滑动节流100ms测试</div>
<div style="height: 100px">滑动节流100ms测试</div>
<div style="height: 100px">滑动节流100ms测试</div>
<div style="height: 100px">滑动节流100ms测试</div>
<div style="height: 100px">滑动节流100ms测试</div>
<div style="height: 100px">滑动节流100ms测试</div>
<div style="height: 100px">滑动节流100ms测试</div>
<div style="height: 100px">滑动节流100ms测试</div>
<div style="height: 100px">滑动节流100ms测试</div>
<div style="height: 100px">滑动节流100ms测试</div>
<div style="height: 100px">滑动节流100ms测试</div>
<div style="height: 100px">滑动节流100ms测试</div>
<div style="height: 100px">滑动节流100ms测试</div>
<div style="height: 100px">滑动节流100ms测试</div>
<div style="height: 100px">滑动节流100ms测试</div>
<div style="height: 100px">滑动节流100ms测试</div>
<div style="height: 100px">滑动节流100ms测试</div>
<div style="height: 100px">滑动节流100ms测试</div>
<div style="height: 100px">滑动节流100ms测试</div>
<div style="height: 100px">滑动节流100ms测试</div>
<div style="height: 100px">滑动节流100ms测试</div>
<div style="height: 100px">滑动节流100ms测试</div>
<div style="height: 100px">滑动节流100ms测试</div>
<div style="height: 100px">滑动节流100ms测试</div>
<div style="height: 100px">滑动节流100ms测试</div>
<div style="height: 100px">滑动节流100ms测试</div>
<div style="height: 100px">滑动节流100ms测试</div>
<div style="height: 100px">滑动节流100ms测试</div>
<div style="height: 100px">滑动节流100ms测试</div>
<div style="height: 100px">滑动节流100ms测试</div>
<div style="height: 100px">到底</div>
<script>
let last = Date.now();
//模拟一段ajax请求
function ajax(content) {
const d = Date.now();
const span = d - last;
console.log(`${content} 间隔 ${span}ms`);
last = d;
}
const noActionInput = document.getElementById('noAction');
noActionInput.addEventListener('keyup', function(e) {
ajax(e.target.value);
});
/**
* 一般利用闭包存储和私有化定时器 `timer`
* 在 `delay` 时间内再次调用则清除未执行的定时器
* 重新定时器
* @param fn
* @param delay
* @returns {Function}
*/
function debounce(fn, delay) {
let timer = null;
return function() {
timer && clearTimeout(timer);
timer = setTimeout(() => fn.apply(this, arguments), delay);
};
}
const debounceInput = document.getElementById('debounce');
const debounceAjax = debounce(ajax, 100);
debounceInput.addEventListener('keyup', function(e) {
debounceAjax(e.target.value);
});
/**
* 时间戳来处理首次和间隔执行问题
* 定会器来确保最后一次状态改变得到执行
* @param fn
* @param delay
* @returns {Function}
*/
function throttle(fn, delay) {
let timer, lastTime;
return function() {
const now = Date.now();
const space = now - lastTime;
if( lastTime && space < delay ) {
timer && clearTimeout(timer);
timer = setTimeout(() => {
lastTime = Date.now();
console.log('setTimeout');
fn.apply(this, arguments);
}, delay - space);
return;
}
lastTime = now;
fn.apply(this, arguments);
timer && clearTimeout(timer);
};
}
const throttleAjax = throttle(ajax, 100);
window.addEventListener('scroll', function() {
const top = document.body.scrollTop || document.documentElement.scrollTop;
throttleAjax('scrollTop: ' + top);
// ajax('scrollTop: ' + top);
});
</script>
</body>
</html>