2 Copyright (c) 2010, Yahoo! Inc. All rights reserved.
3 Code licensed under the BSD License:
4 http://developer.yahoo.com/yui/license.html
8 YUI.add('yui-throttle', function(Y) {
11 * Provides a throttle/limiter for function calls
13 * @submodule yui-throttle
16 /*! Based on work by Simon Willison: http://gist.github.com/292562 */
18 * Throttles a call to a method based on the time between calls.
21 * @param fn {function} The function call to throttle.
22 * @param ms {int} The number of milliseconds to throttle the method call.
23 * Can set globally with Y.config.throttleTime or by call. Passing a -1 will
24 * disable the throttle. Defaults to 150.
25 * @return {function} Returns a wrapped function that calls fn throttled.
28 Y.throttle = function(fn, ms) {
29 ms = (ms) ? ms : (Y.config.throttleTime || 150);
33 fn.apply(null, arguments);
37 var last = Y.Lang.now();
40 var now = Y.Lang.now();
41 if (now - last > ms) {
43 fn.apply(null, arguments);
49 }, '3.3.0' ,{requires:['yui-base']});