Skip to content

Instantly share code, notes, and snippets.

@dpalou
Created February 8, 2022 15:01
Show Gist options
  • Save dpalou/365b5339d3c8e0707466c9313fa83d92 to your computer and use it in GitHub Desktop.
Save dpalou/365b5339d3c8e0707466c9313fa83d92 to your computer and use it in GitHub Desktop.
function throttle(func, duration) {
let shouldWait = false
let hasIgnoredCalls = false;
return function (...args) {
if (!shouldWait) {
func.apply(this, args)
shouldWait = true
setTimeout(function () {
shouldWait = false
if (hasIgnoredCalls) {
hasIgnoredCalls = false;
func.apply(this, args);
}
}, duration)
} else {
hasIgnoredCalls = true;
}
}
}
function throttle(func, duration) {
let shouldWait = false
return function (...args) {
if (!shouldWait) {
func.apply(this, args)
shouldWait = true
setTimeout(function () {
shouldWait = false
}, duration)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment