Created
July 5, 2019 12:45
-
-
Save gsmartagence/8f368e32cc0896d6cde38cb1ed389285 to your computer and use it in GitHub Desktop.
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
| { | |
| "name": "throttle.js", | |
| "version": "0.1.0" | |
| } |
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
| /** | |
| * Vanilla Throttle implementation | |
| * https://gist.github.com/peduarte/969217eac456538789e8fac8f45143b4 | |
| * @param {*} func | |
| * @param {*} wait | |
| */ | |
| const throttle = function(callback, wait, immediate = false) { | |
| let timeout = null | |
| let initialCall = true | |
| return function() { | |
| const callNow = immediate && initialCall | |
| const next = () => { | |
| callback.apply(this, arguments) | |
| timeout = null | |
| } | |
| if (callNow) { | |
| initialCall = false | |
| next() | |
| } | |
| if (!timeout) { | |
| timeout = setTimeout(next, wait) | |
| } | |
| } | |
| } | |
| export {throttle}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment