(duration: number, fn: () => void)
| 234 | * Debounces the function call for an interval. |
| 235 | */ |
| 236 | export function debounce(duration: number, fn: () => void): (() => void) & { clear: () => void } { |
| 237 | let timeout: NodeJS.Timer | void; |
| 238 | const debounced = () => { |
| 239 | if (timeout !== undefined) { |
| 240 | clearTimeout(timeout); |
| 241 | } |
| 242 | |
| 243 | timeout = setTimeout(() => { |
| 244 | timeout = undefined; |
| 245 | fn(); |
| 246 | }, duration); |
| 247 | }; |
| 248 | |
| 249 | debounced.clear = () => { |
| 250 | if (timeout) { |
| 251 | clearTimeout(timeout); |
| 252 | timeout = undefined; |
| 253 | } |
| 254 | }; |
| 255 | |
| 256 | return debounced; |
| 257 | } |
| 258 | |
| 259 | /** |
| 260 | * Bisets the array by the predicate. The first return value will be the ones |
no test coverage detected