~ahasenack/lazr-js/lazr-js-11.03-packaging

« back to all changes in this revision

Viewing changes to src-js/lazrjs/yui/yui/yui-throttle.js

  • Committer: Sidnei da Silva
  • Date: 2010-04-08 14:44:59 UTC
  • mfrom: (166.8.13 yui-3.1.0)
  • Revision ID: sidnei.da.silva@canonical.com-20100408144459-qozybvnrgr7iee7k
Merged yui-3.1.0 [r=therve,rockstar] [f=558457].

Updates lazr-js to use YUI 3.1.0.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
Copyright (c) 2010, Yahoo! Inc. All rights reserved.
 
3
Code licensed under the BSD License:
 
4
http://developer.yahoo.com/yui/license.html
 
5
version: 3.1.0
 
6
build: 2026
 
7
*/
 
8
YUI.add('yui-throttle', function(Y) {
 
9
 
 
10
/**
 
11
 * Provides a throttle/limiter for function calls
 
12
 * @module yui
 
13
 * @submodule yui-throttle
 
14
 */
 
15
 
 
16
/**
 
17
 * Throttles a call to a method based on the time between calls.
 
18
 * @method throttle
 
19
 * @for YUI
 
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.
 
24
 * @since 3.1.0
 
25
 */
 
26
 
 
27
/*! Based on work by Simon Willison: http://gist.github.com/292562 */
 
28
 
 
29
var throttle = function(fn, ms) {
 
30
    ms = (ms) ? ms : (Y.config.throttleTime || 150);
 
31
 
 
32
    if (ms === -1) {
 
33
        return (function() {
 
34
            fn.apply(null, arguments);
 
35
        });
 
36
    }
 
37
 
 
38
    var last = (new Date()).getTime();
 
39
 
 
40
    return (function() {
 
41
        var now = (new Date()).getTime();
 
42
        if (now - last > ms) {
 
43
            last = now;
 
44
            fn.apply(null, arguments);
 
45
        }
 
46
    });
 
47
};
 
48
 
 
49
Y.throttle = throttle;
 
50
 
 
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;
 
54
 
 
55
 
 
56
 
 
57
}, '3.1.0' ,{requires:['yui-base']});