-
Notifications
You must be signed in to change notification settings - Fork 0
/
防抖函数-debounce.html
63 lines (61 loc) · 1.97 KB
/
防抖函数-debounce.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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>防抖函数-debounce</title>
</head>
<body>
<div>
<span>原始版本:</span>
<input type="text" id="input" />
</div>
<div>
<span>防抖版本:</span>
<input type="text" id="debounce" />
</div>
<div>
<span>封装版本:</span>
<input type="text" id="function" />
</div>
<script>
//原始版本
const element = document.getElementById("input");
element.onkeyup = (event) => {
console.log("原始版本--发送网络请求:", event.target.value);
};
</script>
<script>
//防抖版本
const element2 = document.getElementById("debounce");
let timer = null;
element2.onkeyup = (event) => {
if (timer) {
window.clearTimeout(timer);
}
timer = setTimeout(() => {
console.log("防抖版本--发送网络请求:", event.target.value);
}, 1000);
};
</script>
<script>
//封装版本
const element3 = document.getElementById("function");
element3.onkeyup = debounce((event) => {
console.log("封装版本--发送网络请求:", event.target.value);
}, 1000);
function debounce(fun, delay = 500) {
let timer = null;
return function () {
if (timer) {
window.clearTimeout(timer);
}
timer = setTimeout(() => {
//无法传递event参数
// fun();
fun.apply(this, arguments);
}, 1000);
};
}
</script>
</body>
</html>