Skip to content

Instantly share code, notes, and snippets.

@abhinavnigam2207
Created March 5, 2019 12:22
Show Gist options
  • Save abhinavnigam2207/a147abe0213d60467abacd33db7c6d2e to your computer and use it in GitHub Desktop.
Save abhinavnigam2207/a147abe0213d60467abacd33db7c6d2e to your computer and use it in GitHub Desktop.
Throttle Polyfill
const throttle = (func, limit) => {
let lastFunc
let lastRan
return function() {
const context = this
const args = arguments
if (!lastRan) {
func.apply(context, args)
lastRan = Date.now()
} else {
clearTimeout(lastFunc)
lastFunc = setTimeout(function() {
if ((Date.now() - lastRan) >= limit) {
func.apply(context, args)
lastRan = Date.now()
}
}, limit - (Date.now() - lastRan))
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment