Last active
October 15, 2025 03:47
-
-
Save cap340/49c7d131e6d9d11679c4b940e6e95881 to your computer and use it in GitHub Desktop.
A typescript simple debounce function
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * @example | |
| * ```ts | |
| * const textInput = document.querySelector<HTMLInputElement>('input[type="text"]'); | |
| * textInput.addEventListener('keydown', debounce(fetchSuggestions, 300)); | |
| * ``` | |
| * | |
| * @param {T} callback - The callback function to be debounced. | |
| * @param number delay - the delay in milliseconds | |
| */ | |
| export const debounce = <T extends unknown[]>( | |
| callback: (...args: T) => void, | |
| delay: number, | |
| ): ((...args: T) => void) => { | |
| let timeoutTimer: ReturnType<typeof setTimeout>; | |
| return (...args: T) => { | |
| clearTimeout(timeoutTimer); | |
| timeoutTimer = setTimeout(() => { | |
| callback(...args); | |
| }, delay); | |
| }; | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment