src/cm/media/js/lib/yui/yui3-3.15.0/build/yui-throttle/yui-throttle.js
author ymh <ymh.work@gmail.com>
Fri, 14 Mar 2014 13:16:10 +0100
changeset 611 fa66f4bb1563
parent 602 e16a97fb364a
permissions -rw-r--r--
add some more custom config and put every thing in comment in the custom.yaml template
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
602
e16a97fb364a Use YUI 3.15
gibus
parents:
diff changeset
     1
YUI.add('yui-throttle', function (Y, NAME) {
e16a97fb364a Use YUI 3.15
gibus
parents:
diff changeset
     2
e16a97fb364a Use YUI 3.15
gibus
parents:
diff changeset
     3
/**
e16a97fb364a Use YUI 3.15
gibus
parents:
diff changeset
     4
Throttles a call to a method based on the time between calls. This method is attached
e16a97fb364a Use YUI 3.15
gibus
parents:
diff changeset
     5
to the `Y` object and is <a href="../classes/YUI.html#method_throttle">documented there</a>.
e16a97fb364a Use YUI 3.15
gibus
parents:
diff changeset
     6
e16a97fb364a Use YUI 3.15
gibus
parents:
diff changeset
     7
    var fn = Y.throttle(function() {
e16a97fb364a Use YUI 3.15
gibus
parents:
diff changeset
     8
        counter++;
e16a97fb364a Use YUI 3.15
gibus
parents:
diff changeset
     9
    });
e16a97fb364a Use YUI 3.15
gibus
parents:
diff changeset
    10
e16a97fb364a Use YUI 3.15
gibus
parents:
diff changeset
    11
    for (i; i< 35000; i++) {
e16a97fb364a Use YUI 3.15
gibus
parents:
diff changeset
    12
        out++;
e16a97fb364a Use YUI 3.15
gibus
parents:
diff changeset
    13
        fn();
e16a97fb364a Use YUI 3.15
gibus
parents:
diff changeset
    14
    }
e16a97fb364a Use YUI 3.15
gibus
parents:
diff changeset
    15
e16a97fb364a Use YUI 3.15
gibus
parents:
diff changeset
    16
e16a97fb364a Use YUI 3.15
gibus
parents:
diff changeset
    17
@module yui
e16a97fb364a Use YUI 3.15
gibus
parents:
diff changeset
    18
@submodule yui-throttle
e16a97fb364a Use YUI 3.15
gibus
parents:
diff changeset
    19
*/
e16a97fb364a Use YUI 3.15
gibus
parents:
diff changeset
    20
e16a97fb364a Use YUI 3.15
gibus
parents:
diff changeset
    21
/*! Based on work by Simon Willison: http://gist.github.com/292562 */
e16a97fb364a Use YUI 3.15
gibus
parents:
diff changeset
    22
/**
e16a97fb364a Use YUI 3.15
gibus
parents:
diff changeset
    23
 * Throttles a call to a method based on the time between calls.
e16a97fb364a Use YUI 3.15
gibus
parents:
diff changeset
    24
 * @method throttle
e16a97fb364a Use YUI 3.15
gibus
parents:
diff changeset
    25
 * @for YUI
e16a97fb364a Use YUI 3.15
gibus
parents:
diff changeset
    26
 * @param fn {function} The function call to throttle.
e16a97fb364a Use YUI 3.15
gibus
parents:
diff changeset
    27
 * @param ms {Number} The number of milliseconds to throttle the method call.
e16a97fb364a Use YUI 3.15
gibus
parents:
diff changeset
    28
 * Can set globally with Y.config.throttleTime or by call. Passing a -1 will
e16a97fb364a Use YUI 3.15
gibus
parents:
diff changeset
    29
 * disable the throttle. Defaults to 150.
e16a97fb364a Use YUI 3.15
gibus
parents:
diff changeset
    30
 * @return {function} Returns a wrapped function that calls fn throttled.
e16a97fb364a Use YUI 3.15
gibus
parents:
diff changeset
    31
 * @since 3.1.0
e16a97fb364a Use YUI 3.15
gibus
parents:
diff changeset
    32
 */
e16a97fb364a Use YUI 3.15
gibus
parents:
diff changeset
    33
Y.throttle = function(fn, ms) {
e16a97fb364a Use YUI 3.15
gibus
parents:
diff changeset
    34
    ms = (ms) ? ms : (Y.config.throttleTime || 150);
e16a97fb364a Use YUI 3.15
gibus
parents:
diff changeset
    35
e16a97fb364a Use YUI 3.15
gibus
parents:
diff changeset
    36
    if (ms === -1) {
e16a97fb364a Use YUI 3.15
gibus
parents:
diff changeset
    37
        return function() {
e16a97fb364a Use YUI 3.15
gibus
parents:
diff changeset
    38
            fn.apply(this, arguments);
e16a97fb364a Use YUI 3.15
gibus
parents:
diff changeset
    39
        };
e16a97fb364a Use YUI 3.15
gibus
parents:
diff changeset
    40
    }
e16a97fb364a Use YUI 3.15
gibus
parents:
diff changeset
    41
e16a97fb364a Use YUI 3.15
gibus
parents:
diff changeset
    42
    var last = Y.Lang.now();
e16a97fb364a Use YUI 3.15
gibus
parents:
diff changeset
    43
e16a97fb364a Use YUI 3.15
gibus
parents:
diff changeset
    44
    return function() {
e16a97fb364a Use YUI 3.15
gibus
parents:
diff changeset
    45
        var now = Y.Lang.now();
e16a97fb364a Use YUI 3.15
gibus
parents:
diff changeset
    46
        if (now - last > ms) {
e16a97fb364a Use YUI 3.15
gibus
parents:
diff changeset
    47
            last = now;
e16a97fb364a Use YUI 3.15
gibus
parents:
diff changeset
    48
            fn.apply(this, arguments);
e16a97fb364a Use YUI 3.15
gibus
parents:
diff changeset
    49
        }
e16a97fb364a Use YUI 3.15
gibus
parents:
diff changeset
    50
    };
e16a97fb364a Use YUI 3.15
gibus
parents:
diff changeset
    51
};
e16a97fb364a Use YUI 3.15
gibus
parents:
diff changeset
    52
e16a97fb364a Use YUI 3.15
gibus
parents:
diff changeset
    53
e16a97fb364a Use YUI 3.15
gibus
parents:
diff changeset
    54
}, '@VERSION@', {"requires": ["yui-base"]});