-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paththrottle.js
47 lines (42 loc) · 1.02 KB
/
throttle.js
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
export const throttle = (
func,
wait,
option = { leading: true, trailing: true }
) => {
const { leading, trailing } = option
let waiting = false,
stashed = null
function startTimer() {
waiting = true
setTimeout(() => {
waiting = false
// 时间片结束时
// 根据 stashed 触发 trailing
// 否则结束
if (stashed) {
func.apply(stashed[0], stashed[1])
stashed = null
startTimer()
}
}, wait)
}
return function (...args) {
// 时间片运行过程中,再次调用
// 仅 trailing 时,记录时间片结束时需要的数据
if (waiting) {
if (trailing) stashed = [this, args]
return
}
// 首次调用: leading 时,主动触发一次调用,启动计时
if (leading) {
func.apply(this, args)
startTimer()
return
}
// 首次调用: trailing 时,记录时间片结束时需要的数据,启动计时
if (trailing) {
stashed = [this, args]
startTimer()
}
}
}