| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950 |
- /*
- Copyright (c) 2010, Yahoo! Inc. All rights reserved.
- Code licensed under the BSD License:
- http://developer.yahoo.com/yui/license.html
- version: 3.4.0
- build: nightly
- */
- YUI.add('yui-throttle', function(Y) {
-
- /**
- * Throttles a call to a method based on the time between calls. This method is attached
- * to the `Y` object and is <a href="../classes/YUI.html#method_throttle">documented there</a>.
- * @module yui
- * @submodule yui-throttle
- */
-
- /*! Based on work by Simon Willison: http://gist.github.com/292562 */
- /**
- * Throttles a call to a method based on the time between calls.
- * @method throttle
- * @for YUI
- * @param fn {function} The function call to throttle.
- * @param ms {int} The number of milliseconds to throttle the method call.
- * Can set globally with Y.config.throttleTime or by call. Passing a -1 will
- * disable the throttle. Defaults to 150.
- * @return {function} Returns a wrapped function that calls fn throttled.
- * @since 3.1.0
- */
- Y.throttle = function(fn, ms) {
- ms = (ms) ? ms : (Y.config.throttleTime || 150);
-
- if (ms === -1) {
- return (function() {
- fn.apply(null, arguments);
- });
- }
-
- var last = Y.Lang.now();
-
- return (function() {
- var now = Y.Lang.now();
- if (now - last > ms) {
- last = now;
- fn.apply(null, arguments);
- }
- });
- };
-
-
- }, '3.4.0' ,{requires:['yui-base']});
|