-
Notifications
You must be signed in to change notification settings - Fork 1
/
debounce.html
77 lines (70 loc) · 2.03 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>防抖和节流</title>
</head>
<body>
<p>debounce防抖:通过设置定时器,让高频触发的事件在触发结束之后再做出相关响应。只执行一次。</p>
<p>使用场景:</p>
<p>resize、scroll、用户点击按钮提交ajax请求的时候。</p>
<div id="common"></div>
<div id="special"></div>
</body>
<script>
/**
*
* 版本一
*/
/* let common = document.getElementById('common'); // 获取页面的左边
let special = document.getElementById('special'); // 获取页面的右边
// 执行debounce
window.onresize = function () {
debounce(addlist, 300); // 设置了防抖
commonWay(); // 普通的函数执行
}
function debounce(fn,delay) {
clearTimeout(fn.timeid);
fn.timeid = setTimeout(function() {
fn()
},delay)
}
function addlist () {
special.innerHTML += '<li>debounce</li>';
}
function commonWay () {
common.innerHTML += '<li>k</li>'
} */
</script>
<script>
/**
*
* 版本二
*/
let common = document.getElementById('common'); // 获取页面的左边
let special = document.getElementById('special'); // 获取页面的右边
let middle = debounce(addlist, 300);
// 执行debounce
window.addEventListener('resize',function (){
middle()
commonWay(); // 普通的函数执行
})
function debounce(fn,delay) {
let timeid; // 设置一个定时器
return function () {
clearTimeout(timeid);
timeid = setTimeout(function() {
fn()
},delay)
}
}
function addlist () {
special.innerHTML += '<li>debounce</li>';
}
function commonWay () {
common.innerHTML += '<li>k</li>'
}
</script>
</html>