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