Files
SnowLuma/.pnpm-store/v11/files/54/59cc118cfb33c0fa6e1e1015beef4d4193d483207456b5ddc52915854edd86608deda837838bd950c6437bc12ff66fce1e372a0b8bc5943caafc4146a7e69c
TanTan 2a69b23b47
Dev Build / cleanup (push) Canceled after 0s
Dev Build / typecheck (push) Canceled after 0s
Dev Build / build (linux-arm64, ubuntu-24.04-arm, linux-arm64) (push) Canceled after 0s
Dev Build / build (linux-x64, ubuntu-latest, linux-x64) (push) Canceled after 0s
Dev Build / build (win-x64, windows-latest, win32-x64) (push) Canceled after 0s
Dev Build / dispatch-dev-docker (push) Canceled after 0s
docs: 仓库链接指向本镜像仓库
2026-11-13 09:27:07 +08:00

37 lines
1.2 KiB
Plaintext

export type Procedure = (...args: any[]) => void
/**
* @link https://davidwalsh.name/javascript-debounce-function
* @param func needs to implement a function which is debounced
* @param wait how long do you want to wait till the previous declared function is executed
* @param isImmediate defines if you want to execute the function on the first execution or the last execution inside the time window. `true` for first and `false` for last.
*/
export default <F extends Procedure>(
func: F,
wait: number,
isImmediate?: boolean,
): ((this: ThisParameterType<F>, ...args: Parameters<F>) => void) => {
let timeout: ReturnType<typeof setTimeout> | undefined
return function debounce(this: ThisParameterType<F>, ...args: Parameters<F>) {
// eslint-disable-next-line @typescript-eslint/no-this-alias
const context = this
const later = () => {
timeout = undefined
if (!isImmediate) {
func.apply(context, args)
}
}
const shouldCallNow = isImmediate && !timeout
if (timeout !== undefined) {
clearTimeout(timeout)
}
timeout = setTimeout(later, wait)
if (shouldCallNow) {
func.apply(context, args)
return
}
return undefined
}
}