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