]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - jssource/src_files/include/javascript/yui3/build/yui/yui-throttle.js
Release 6.5.0
[Github/sugarcrm.git] / jssource / src_files / include / javascript / yui3 / build / yui / yui-throttle.js
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.3.0
6 build: 3167
7 */
8 YUI.add('yui-throttle', function(Y) {
9
10 /**
11  * Provides a throttle/limiter for function calls
12  * @module yui
13  * @submodule yui-throttle
14  */
15
16 /*! Based on work by Simon Willison: http://gist.github.com/292562 */
17 /**
18  * Throttles a call to a method based on the time between calls.
19  * @method throttle
20  * @for YUI
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.
26  * @since 3.1.0
27  */
28 Y.throttle = function(fn, ms) {
29     ms = (ms) ? ms : (Y.config.throttleTime || 150);
30
31     if (ms === -1) {
32         return (function() {
33             fn.apply(null, arguments);
34         });
35     }
36
37     var last = Y.Lang.now();
38
39     return (function() {
40         var now = Y.Lang.now();
41         if (now - last > ms) {
42             last = now;
43             fn.apply(null, arguments);
44         }
45     });
46 };
47
48
49 }, '3.3.0' ,{requires:['yui-base']});