Newer
Older
CVSS_3.0_GUI / node_modules / nwjs-builder-phoenix / node_modules / request-progress / node_modules / throttleit / index.js
root on 7 May 2019 820 bytes Initial commit
  1. module.exports = throttle;
  2.  
  3. /**
  4. * Returns a new function that, when invoked, invokes `func` at most once per `wait` milliseconds.
  5. *
  6. * @param {Function} func Function to wrap.
  7. * @param {Number} wait Number of milliseconds that must elapse between `func` invocations.
  8. * @return {Function} A new function that wraps the `func` function passed in.
  9. */
  10.  
  11. function throttle (func, wait) {
  12. var ctx, args, rtn, timeoutID; // caching
  13. var last = 0;
  14.  
  15. return function throttled () {
  16. ctx = this;
  17. args = arguments;
  18. var delta = new Date() - last;
  19. if (!timeoutID)
  20. if (delta >= wait) call();
  21. else timeoutID = setTimeout(call, wait - delta);
  22. return rtn;
  23. };
  24.  
  25. function call () {
  26. timeoutID = 0;
  27. last = +new Date();
  28. rtn = func.apply(ctx, args);
  29. ctx = null;
  30. args = null;
  31. }
  32. }
Buy Me A Coffee