|
@@ -31,7 +31,7 @@
|
|
|
* delay: 消抖时长
|
|
* delay: 消抖时长
|
|
|
* isImmediateCall: 是在第一次调用时立即执行fn,还是在最后一次调用后等delay时长再调用fn
|
|
* isImmediateCall: 是在第一次调用时立即执行fn,还是在最后一次调用后等delay时长再调用fn
|
|
|
*/
|
|
*/
|
|
|
- export function debounce(fn, delay, isImmediateCall = true) {
|
|
|
|
|
|
|
+export function debounce(fn, delay, isImmediateCall = false) {
|
|
|
let timer = null
|
|
let timer = null
|
|
|
// 上次调用的时刻
|
|
// 上次调用的时刻
|
|
|
let lastCallTime = 0
|
|
let lastCallTime = 0
|
|
@@ -47,15 +47,13 @@
|
|
|
}
|
|
}
|
|
|
} else {
|
|
} else {
|
|
|
return function (...args) {
|
|
return function (...args) {
|
|
|
- const context = this
|
|
|
|
|
- const currentTime = Date.now()
|
|
|
|
|
- if (currentTime - lastCallTime >= delay) {
|
|
|
|
|
|
|
+ if (timer) {
|
|
|
clearTimeout(timer)
|
|
clearTimeout(timer)
|
|
|
- timer = setTimeout(() => {
|
|
|
|
|
- fn.apply(context, args)
|
|
|
|
|
- }, delay)
|
|
|
|
|
}
|
|
}
|
|
|
- lastCallTime = currentTime
|
|
|
|
|
|
|
+ const context = this
|
|
|
|
|
+ timer = setTimeout(() => {
|
|
|
|
|
+ fn.apply(context, args)
|
|
|
|
|
+ }, delay)
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|