[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

/lib/yuilib/3.17.2/yui-throttle/ -> yui-throttle.js (source)

   1  /*
   2  YUI 3.17.2 (build 9c3c78e)
   3  Copyright 2014 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 {Number} 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.17.2', {"requires": ["yui-base"]});


Generated: Thu Aug 11 10:00:09 2016 Cross-referenced by PHPXref 0.7.1