-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdebounce.html
More file actions
60 lines (54 loc) · 1.48 KB
/
debounce.html
File metadata and controls
60 lines (54 loc) · 1.48 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
<!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>debounce</title>
</head>
<body>
<p class="info">防抖(debounce):要等你触发完事件 n 秒内不再触发事件,我才执行</p>
<div id="container">
</div>
</body>
<style>
.info {
text-align: center;
width: 100%;
}
body {
margin: 0;
}
#container {
height: 200px;
width: 100%;
display: flex;
justify-content: center;
align-items: center;
background: #444;
color: #ffffff;
font-size: 3rem;
}
</style>
<script>
let count = 1;
let container = document.getElementById('container');
container.innerHTML = count
const getUserAction = (e) => {
console.log(e);
container.innerHTML = count++;
};
// 有定时器 => 清除之前的定时器,重新设置一个新定时器调用传入的函数
// 没有定时器 => 设置一个定时器调用传入的函数
const debounce = (callback, waitTime) => {
return function () {
let args = arguments;
if (callback.fid) { clearTimeout(callback.fid) }
callback.fid = setTimeout(() => {
callback.apply(this, args)
}, waitTime)
}
}
container.onmousemove = debounce(getUserAction, 1000)
</script>
</html>