Created
November 16, 2017 03:25
-
-
Save ICEDLEE337/a29a2c4233c92e529b0623cad4310e93 to your computer and use it in GitHub Desktop.
example of using lodash's `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
(() => { | |
// in this example we invoke a fn for a period of 10 sec, invoking it 10 times a second, but we can perceive that the original function is only invoked at most once per second according to the parameter below | |
var TOTAL_TIME_TO_RUN = 10000; // 10 sec | |
var THROTTLE_INTERVAL = 2000; // <= adjust this number to see throttling in action | |
var INVOCATION_INTERVAL = 100; // 0.1 sec | |
// regular fn | |
var punchClock = function punchClock () { | |
console.log(new Date().toISOString()); | |
}; | |
// wrap it and supply interval representing minimum delay between invocations | |
var throttledPunchClock = _.throttle(punchClock, THROTTLE_INTERVAL); | |
// set up looping | |
var intervalId = setInterval(throttledPunchClock, INVOCATION_INTERVAL); | |
// run the demo | |
setTimeout(()=>clearInterval(intervalId), 10000) | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment