Created
February 8, 2022 15:01
-
-
Save dpalou/365b5339d3c8e0707466c9313fa83d92 to your computer and use it in GitHub Desktop.
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(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; | |
} | |
} | |
} |
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(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