Version 3.18.1
Show:

File: yui-throttle/js/throttle.js

  1. /**
  2. Throttles a call to a method based on the time between calls. This method is attached
  3. to the `Y` object and is <a href="../classes/YUI.html#method_throttle">documented there</a>.
  4. var fn = Y.throttle(function() {
  5. counter++;
  6. });
  7. for (i; i< 35000; i++) {
  8. out++;
  9. fn();
  10. }
  11. @module yui
  12. @submodule yui-throttle
  13. */
  14. /*! Based on work by Simon Willison: http://gist.github.com/292562 */
  15. /**
  16. * Throttles a call to a method based on the time between calls.
  17. * @method throttle
  18. * @for YUI
  19. * @param fn {function} The function call to throttle.
  20. * @param ms {Number} The number of milliseconds to throttle the method call.
  21. * Can set globally with Y.config.throttleTime or by call. Passing a -1 will
  22. * disable the throttle. Defaults to 150.
  23. * @return {function} Returns a wrapped function that calls fn throttled.
  24. * @since 3.1.0
  25. */
  26. Y.throttle = function(fn, ms) {
  27. ms = (ms) ? ms : (Y.config.throttleTime || 150);
  28. if (ms === -1) {
  29. return function() {
  30. fn.apply(this, arguments);
  31. };
  32. }
  33. var last = Y.Lang.now();
  34. return function() {
  35. var now = Y.Lang.now();
  36. if (now - last > ms) {
  37. last = now;
  38. fn.apply(this, arguments);
  39. }
  40. };
  41. };