Skip to content

Instantly share code, notes, and snippets.

@WomB0ComB0
Created July 1, 2024 02:54
Show Gist options
  • Save WomB0ComB0/322b67c2c865b41e9eb9136a222359ab to your computer and use it in GitHub Desktop.
Save WomB0ComB0/322b67c2c865b41e9eb9136a222359ab to your computer and use it in GitHub Desktop.
Debounce function in Typescript
/**
* 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}`;
}
@WomB0ComB0
Copy link
Author

My bad, this was private

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