防抖、节流
# 防抖、节流
// 节流
function throttle(fn, interval) {
let flag = true;
return function(...args) {
if (flag){
flag = false;
setTimeout(() => {
fn.apply(this, args);
flag = true;
}, interval);
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
2
3
4
5
6
7
8
9
10
11
12
13
// 防抖
function debounce(fn, interval) {
let timer = null;
return function (...args) {
if(timer) clearTimeout(timer);
timer = setTimeout(() => {
fn.apply(this, args);
}, interval);
}
}
1
2
3
4
5
6
7
8
9
10
2
3
4
5
6
7
8
9
10