Skip to content

Instantly share code, notes, and snippets.

@cagataycali
Created November 24, 2021 04:08
Show Gist options
  • Save cagataycali/cf2e70c3654365355485961421bae0d9 to your computer and use it in GitHub Desktop.
Save cagataycali/cf2e70c3654365355485961421bae0d9 to your computer and use it in GitHub Desktop.
[JavaScript] throttle
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