Skip to content

Instantly share code, notes, and snippets.

@fmanimashaun
Created July 14, 2024 10:17
Show Gist options
  • Save fmanimashaun/d02b9b7133e6abecaf2a92cf13cad39e to your computer and use it in GitHub Desktop.
Save fmanimashaun/d02b9b7133e6abecaf2a92cf13cad39e to your computer and use it in GitHub Desktop.
Custom Debounce Function in TypeScript
// 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");
@fmanimashaun
Copy link
Author

Here is the implementation of a debounce function in typescript

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment