-
-
Save PatrykBuniX/0f4d5fb348e4de620662d25cd727a82c to your computer and use it in GitHub Desktop.
Implementation of throttle function in TypeScript
This file contains 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
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