0%

防抖 节流

摘要:防抖 节流通用方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
 // 节流
throttle(fn, delay = 500) {
return () => {
if (this.throttleTimer) {
return
}
this.throttleTimer = setTimeout((...res) => {
fn.apply(this, [...res])
this.throttleTimer = null
}, delay)
}
},
// 防抖
debounce(fn, delay = 500) {
return () => {
if (this.debounceTimer) {
clearTimeout(this.debounceTimer)
}
this.debounceTimer = setTimeout((...res) => {
fn.apply(this, [...res])
this.debounceTimer = null
}, delay)
}
}
-------------本文结束感谢您的阅读-------------