Guest User

Untitled

a guest
Nov 18th, 2017
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.80 KB | None | 0 0
  1. (() => {
  2.  
  3. // 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
  4.  
  5. var TOTAL_TIME_TO_RUN = 10000; // 10 sec
  6. var THROTTLE_INTERVAL = 2000; // <= adjust this number to see throttling in action
  7. var INVOCATION_INTERVAL = 100; // 0.1 sec
  8.  
  9. // regular fn
  10. var punchClock = function punchClock () {
  11. console.log(new Date().toISOString());
  12. };
  13.  
  14. // wrap it and supply interval representing minimum delay between invocations
  15. var throttledPunchClock = _.throttle(punchClock, THROTTLE_INTERVAL);
  16.  
  17. // set up looping
  18. var intervalId = setInterval(throttledPunchClock, INVOCATION_INTERVAL);
  19.  
  20. // run the demo
  21. setTimeout(()=>clearInterval(intervalId), 10000)
  22.  
  23. })();
Add Comment
Please, Sign In to add comment