Dashboard sipadu mbip
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

yui-throttle.js 1.4KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. /*
  2. Copyright (c) 2010, Yahoo! Inc. All rights reserved.
  3. Code licensed under the BSD License:
  4. http://developer.yahoo.com/yui/license.html
  5. version: 3.4.0
  6. build: nightly
  7. */
  8. YUI.add('yui-throttle', function(Y) {
  9. /**
  10. * Throttles a call to a method based on the time between calls. This method is attached
  11. * to the `Y` object and is <a href="../classes/YUI.html#method_throttle">documented there</a>.
  12. * @module yui
  13. * @submodule yui-throttle
  14. */
  15. /*! Based on work by Simon Willison: http://gist.github.com/292562 */
  16. /**
  17. * Throttles a call to a method based on the time between calls.
  18. * @method throttle
  19. * @for YUI
  20. * @param fn {function} The function call to throttle.
  21. * @param ms {int} The number of milliseconds to throttle the method call.
  22. * Can set globally with Y.config.throttleTime or by call. Passing a -1 will
  23. * disable the throttle. Defaults to 150.
  24. * @return {function} Returns a wrapped function that calls fn throttled.
  25. * @since 3.1.0
  26. */
  27. Y.throttle = function(fn, ms) {
  28. ms = (ms) ? ms : (Y.config.throttleTime || 150);
  29. if (ms === -1) {
  30. return (function() {
  31. fn.apply(null, arguments);
  32. });
  33. }
  34. var last = Y.Lang.now();
  35. return (function() {
  36. var now = Y.Lang.now();
  37. if (now - last > ms) {
  38. last = now;
  39. fn.apply(null, arguments);
  40. }
  41. });
  42. };
  43. }, '3.4.0' ,{requires:['yui-base']});