export function throttle any>(func: T, wait: number): (...args: Parameters) => void { let timeout: NodeJS.Timeout | null = null; let lastArgs: Parameters | null = null; let lastCallTime = 0; return function executedFunction(...args: Parameters) { const now = Date.now(); const remaining = wait - (now - lastCallTime); lastArgs = args; if (remaining <= 0) { lastCallTime = now; func(...args); lastArgs = null; } else if (!timeout) { timeout = setTimeout(() => { lastCallTime = Date.now(); timeout = null; if (lastArgs) { func(...lastArgs); lastArgs = null; } }, remaining); } }; } export function throttleWithTrailing any>( func: T, wait: number, ): (...args: Parameters) => void { let timeout: NodeJS.Timeout | null = null; let lastArgs: Parameters | null = null; let lastCallTime = 0; return function executedFunction(...args: Parameters) { const now = Date.now(); const remaining = wait - (now - lastCallTime); lastArgs = args; if (remaining <= 0) { if (timeout) { clearTimeout(timeout); timeout = null; } lastCallTime = now; func(...args); } else if (!timeout) { timeout = setTimeout(() => { lastCallTime = Date.now(); timeout = null; if (lastArgs) { func(...lastArgs); lastArgs = null; } }, remaining); } }; }