-
Notifications
You must be signed in to change notification settings - Fork 0
/
debouch.html
67 lines (67 loc) · 1.37 KB
/
debouch.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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
<script type="text/javascript">
// 防抖的请求间隔是受用户自身操作影响
function debounch(fun, wait, imadiate){
let timer, context, args;
let later=()=>setTimeout(()=>{
timer=null;
if(!imadiate){
//处理不立即执行的情况
console.log(imadiate)
fun.apply(context, args);
context=args=null;
}
},wait)
return function(...rest){
console.log(rest)
if(timer){
clearTimeout(timer)
timer=later()
}else {
timer=later()
if(imadiate){
fun.apply(this,rest)
}else{
//在不立即执行情况下 将this环境和参数传递给later里
context=this;
args=rest;
}
}
}
}
let b=debounch(function(a){
console.log(1111111,a)
}, 500, true)
function debounch(fn, wait, immadiate){
let context, args, timer;
let later=()=>setTimeout(()=>{
timer=null;
if(!immadiate){
fn.apply(context, args);
context=args=null
}
},wait);
return function(...rest){
if(timer){
clearTimeout(timer);
timer=later();
}else{
timer=later();
if(immadiate){
fn.apply(this, rest)
}else{
context=this;
args=rest;
}
}
}
}
</script>
</body>
</html>