Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- (() => {
- // 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)
- })();
Add Comment
Please, Sign In to add comment