Created
July 14, 2024 10:17
-
-
Save fmanimashaun/d02b9b7133e6abecaf2a92cf13cad39e to your computer and use it in GitHub Desktop.
Custom Debounce Function in TypeScript
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
// debounce.ts | |
const debounce = (callback: Function, delay: number): Function => { | |
let intervalId: number | NodeJS.Timeout; | |
return (...args: any) => { | |
if (intervalId) { | |
clearTimeout(intervalId); | |
} | |
intervalId = setTimeout(() => { | |
callback(...args); | |
}, delay); | |
}; | |
}; | |
// usage | |
const queryApi = (str: string): void => { | |
console.log(`Calling api with string ${str}`); | |
}; | |
const debounceApi = debounce(queryApi, 1000); | |
debounceApi("here"); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Here is the implementation of a debounce function in typescript