throttle.js
903 Bytes
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
/**
* 防抖函数 防止多次点击执行多次方法,可用于防重复提交
*/
const debounce = (fn,delay = 100,promptly) => {
let timer = null;
return function(...args) {
// 立即执行
if(promptly) {
// 当timer为null时执行
if(!timer) fn.apply(this,args);
if(timer) {
clearTimeout(timer)
}
timer = setTimeout(() => {
timer = null;
},delay)
}else {
if(timer) {
clearTimeout(timer)
}
timer = setTimeout(() => {
fn.apply(this,args);
},delay)
}
}
}
/**
* 节流函数 每间隔设置时间执行一次,可用于页面滚动执行操作
*/
const throttle = (fn,delay = 100) => {
let valid = true;
return function(...args) {
if(!valid) {
return
}
valid = false;
fn.apply(this,args)
setTimeout(() => {
valid = true
},delay)
}
}
export {
debounce,
throttle
}