Created
November 24, 2021 04:08
-
-
Save cagataycali/cf2e70c3654365355485961421bae0d9 to your computer and use it in GitHub Desktop.
[JavaScript] throttle
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 (callback, limit) { | |
var waiting = false; // Initially, we're not waiting | |
return function () { // We return a throttled function | |
if (!waiting) { // If we're not waiting | |
callback.apply(this, arguments); // Execute users function | |
waiting = true; // Prevent future invocations | |
setTimeout(function () { // After a period of time | |
waiting = false; // And allow future invocations | |
}, limit); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment