export function sleep(ms = 0): Promise { return new Promise(resolve => setTimeout(resolve, ms)) } export function debounce(func: (this: Receiver, ...args: Args) => unknown, duration: number) { let timeout: ReturnType | undefined return function (this: Receiver, ...args: Args): void { const effect = () => { timeout = undefined return func.apply(this, args) } clearTimeout(timeout) timeout = setTimeout(effect, duration) } } export function throttle(func: (this: Receiver, ...args: Args) => unknown, duration: number) { let shouldWait = false return function (this: Receiver, ...args: Args): void { if (!shouldWait) { func.apply(this, args) shouldWait = true setTimeout(() => { shouldWait = false }, duration) } } }