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
17
* Throttles a call to a method based on the time between calls.
20
* @param fn {function} The function call to throttle.
21
* @param ms {int} The number of milliseconds to throttle the method call. Can set
22
* globally with Y.config.throttleTime or by call. Passing a -1 will disable the throttle. Defaults to 150
23
* @return {function} Returns a wrapped function that calls fn throttled.
27
/*! Based on work by Simon Willison: http://gist.github.com/292562 */
29
var throttle = function(fn, ms) {
30
ms = (ms) ? ms : (Y.config.throttleTime || 150);
34
fn.apply(null, arguments);
38
var last = (new Date()).getTime();
41
var now = (new Date()).getTime();
42
if (now - last > ms) {
44
fn.apply(null, arguments);
49
Y.throttle = throttle;
51
// We added the redundant definition to later for backwards compatibility.
52
// I don't think we need to do the same thing here
53
// Y.Lang.throttle = throttle;
57
}, '3.1.0' ,{requires:['yui-base']});