Skip to content

Instantly share code, notes, and snippets.

@PatrykBuniX
Last active January 30, 2022 16:56
Show Gist options
  • Save PatrykBuniX/0f4d5fb348e4de620662d25cd727a82c to your computer and use it in GitHub Desktop.
Save PatrykBuniX/0f4d5fb348e4de620662d25cd727a82c to your computer and use it in GitHub Desktop.
Implementation of throttle function in TypeScript
function throttle<T extends any[]>(
func: (...args: T) => void,
delay: number
) {
let throttleTimeoutId: ReturnType<typeof setTimeout> | undefined;
return function (...args: T) {
if (throttleTimeoutId) return;
throttleTimeoutId = setTimeout(() => {
func(...args);
throttleTimeoutId = undefined;
}, delay);
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment