Created
July 1, 2024 02:54
-
-
Save WomB0ComB0/322b67c2c865b41e9eb9136a222359ab to your computer and use it in GitHub Desktop.
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
/** | |
* Debounces a function by delaying its execution until a certain amount of time has passed | |
* since the last time it was invoked. Only the last invocation within the delay period will be executed. | |
*/ | |
export function debounce(fn: Function, time: number = 300): Function { | |
let timeoutId: ReturnType<typeof setTimeout>; | |
return function (this: any, ...args: any[]) { | |
clearTimeout(timeoutId); | |
timeoutId = setTimeout(() => { | |
fn.apply(this, args); | |
}, time); | |
}; | |
} | |
interface QueryParams { | |
[key: string]: string | number; | |
} | |
export function generateUrl(baseUrl: string, queryParams: QueryParams): string { | |
const queryString = Object.entries(queryParams) | |
.map( | |
([key, value]) => | |
`${encodeURIComponent(key)}=${encodeURIComponent(String(value))}` | |
) | |
.join("&"); | |
return `${baseUrl}?${queryString}`; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
My bad, this was private