-
-
Save sagartalla/2814772f147077c89fcfc3b12db7934c to your computer and use it in GitHub Desktop.
Throttle Polyfill
This file contains hidden or 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
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