yui-throttle-debug.js revision 952ddc23366b3f5c426f0ca05297144c6166a418
44e29ab1a81842519755c5d3b9a3403efad35354Laszlo HordosYUI.add('yui-throttle', function(Y) {
bdcef189d83ab13d06dde11499a03b16cdb5b432Chad Kienle
bdcef189d83ab13d06dde11499a03b16cdb5b432Chad Kienle/**
bdcef189d83ab13d06dde11499a03b16cdb5b432Chad Kienle * Provides a throttle/limiter for function calls
bdcef189d83ab13d06dde11499a03b16cdb5b432Chad Kienle * @module yui
bdcef189d83ab13d06dde11499a03b16cdb5b432Chad Kienle * @submodule yui-throttle
bdcef189d83ab13d06dde11499a03b16cdb5b432Chad Kienle */
bdcef189d83ab13d06dde11499a03b16cdb5b432Chad Kienle
bdcef189d83ab13d06dde11499a03b16cdb5b432Chad Kienle/**
bdcef189d83ab13d06dde11499a03b16cdb5b432Chad Kienle * Throttles a call to a method based on the time between calls.
bdcef189d83ab13d06dde11499a03b16cdb5b432Chad Kienle * @method throttle
bdcef189d83ab13d06dde11499a03b16cdb5b432Chad Kienle * @for YUI
bdcef189d83ab13d06dde11499a03b16cdb5b432Chad Kienle * @param fn {function} The function call to throttle.
bdcef189d83ab13d06dde11499a03b16cdb5b432Chad Kienle * @param ms {int} The number of milliseconds to throttle the method call. Can set
bdcef189d83ab13d06dde11499a03b16cdb5b432Chad Kienle * globally with Y.config.throttleTime or by call. Passing a -1 will disable the throttle. Defaults to 150
bdcef189d83ab13d06dde11499a03b16cdb5b432Chad Kienle * @return {function} Returns a wrapped function that calls fn throttled.
bdcef189d83ab13d06dde11499a03b16cdb5b432Chad Kienle */
bdcef189d83ab13d06dde11499a03b16cdb5b432Chad Kienle
bdcef189d83ab13d06dde11499a03b16cdb5b432Chad Kienlevar throttle = function(fn, ms) {
bdcef189d83ab13d06dde11499a03b16cdb5b432Chad Kienle if (ms === -1) {
bdcef189d83ab13d06dde11499a03b16cdb5b432Chad Kienle return (function() {
bdcef189d83ab13d06dde11499a03b16cdb5b432Chad Kienle fn.apply(null, arguments);
bdcef189d83ab13d06dde11499a03b16cdb5b432Chad Kienle });
44e29ab1a81842519755c5d3b9a3403efad35354Laszlo Hordos }
44e29ab1a81842519755c5d3b9a3403efad35354Laszlo Hordos ms = Y.config.throttleTime || 150
44e29ab1a81842519755c5d3b9a3403efad35354Laszlo Hordos var last = (new Date()).getTime();
44e29ab1a81842519755c5d3b9a3403efad35354Laszlo Hordos
44e29ab1a81842519755c5d3b9a3403efad35354Laszlo Hordos return (function() {
44e29ab1a81842519755c5d3b9a3403efad35354Laszlo Hordos var now = (new Date()).getTime();
44e29ab1a81842519755c5d3b9a3403efad35354Laszlo Hordos if (now - last > ms) {
44e29ab1a81842519755c5d3b9a3403efad35354Laszlo Hordos last = now;
44e29ab1a81842519755c5d3b9a3403efad35354Laszlo Hordos fn.apply(null, arguments);
44e29ab1a81842519755c5d3b9a3403efad35354Laszlo Hordos }
44e29ab1a81842519755c5d3b9a3403efad35354Laszlo Hordos });
44e29ab1a81842519755c5d3b9a3403efad35354Laszlo Hordos};
44e29ab1a81842519755c5d3b9a3403efad35354Laszlo Hordos
44e29ab1a81842519755c5d3b9a3403efad35354Laszlo HordosY.throttle = throttle;
44e29ab1a81842519755c5d3b9a3403efad35354Laszlo HordosY.Lang.throttle = throttle;
bdcef189d83ab13d06dde11499a03b16cdb5b432Chad Kienle
44e29ab1a81842519755c5d3b9a3403efad35354Laszlo Hordos
bdcef189d83ab13d06dde11499a03b16cdb5b432Chad Kienle
bdcef189d83ab13d06dde11499a03b16cdb5b432Chad Kienle}, '@VERSION@' ,{requires:['yui-base']});
44e29ab1a81842519755c5d3b9a3403efad35354Laszlo Hordos