Skip to content

Instantly share code, notes, and snippets.

@juliozuppa
Created September 21, 2021 18:22
Show Gist options
  • Save juliozuppa/4fec18f98d282cee97065470476ac8c0 to your computer and use it in GitHub Desktop.
Save juliozuppa/4fec18f98d282cee97065470476ac8c0 to your computer and use it in GitHub Desktop.
/**
*
* @param func
* @param duration
* @returns {function(...[*]=): void}
*/
const throttle = function (func, duration) {
let shouldWait = false
return function (...args) {
if (!shouldWait) {
func.apply(this, args)
shouldWait = true
setTimeout(() => {
shouldWait = false
}, duration)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment