chore(防抖节流工具): 新增注释
This commit is contained in:
parent
fe7ffdf7c0
commit
055166551d
@ -6,6 +6,7 @@ export function useDebounce(fn, delay = 500, immediate = false) {
|
|||||||
let instance = getCurrentInstance() // 获取当前组件实例
|
let instance = getCurrentInstance() // 获取当前组件实例
|
||||||
|
|
||||||
const debounced = (...args) => {
|
const debounced = (...args) => {
|
||||||
|
// 接收所有传入的参数
|
||||||
if (timer) clearTimeout(timer)
|
if (timer) clearTimeout(timer)
|
||||||
|
|
||||||
if (immediate && !timer) {
|
if (immediate && !timer) {
|
||||||
@ -14,7 +15,7 @@ export function useDebounce(fn, delay = 500, immediate = false) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
timer = setTimeout(() => {
|
timer = setTimeout(() => {
|
||||||
if (!immediate) fn.apply(this, args)
|
if (!immediate) fn.apply(this, args) // 将参数透传给原始函数
|
||||||
timer = null // 清理timer引用
|
timer = null // 清理timer引用
|
||||||
}, delay)
|
}, delay)
|
||||||
}
|
}
|
||||||
@ -50,7 +51,7 @@ export function useThrottle(fn, delay = 1000) {
|
|||||||
const now = Date.now()
|
const now = Date.now()
|
||||||
if (now - lastCall < delay) return
|
if (now - lastCall < delay) return
|
||||||
lastCall = now
|
lastCall = now
|
||||||
fn(...args)
|
fn(...args) // 将参数透传给原始函数
|
||||||
}
|
}
|
||||||
|
|
||||||
return throttled
|
return throttled
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user