(fn: any, delay = 300, { leading }: { leading?: boolean } = {})
| 7 | * @returns A new debounced function |
| 8 | */ |
| 9 | export function debounce(fn: any, delay = 300, { leading }: { leading?: boolean } = {}) { |
| 10 | let timer: NodeJS.Timeout; |
| 11 | return (...args: Array<any>) => { |
| 12 | if (timer === undefined && leading) { |
| 13 | fn.apply(this, args); |
| 14 | } |
| 15 | clearTimeout(timer); |
| 16 | timer = setTimeout(() => { |
| 17 | fn.apply(this, args); |
| 18 | timer = undefined; |
| 19 | }, delay); |
| 20 | }; |
| 21 | } |
| 22 | |
| 23 | /** |
| 24 | * Creates a throttled function that only invokes the provided function at most once per specified delay |
no test coverage detected