1 |
|
2 |
|
3 |
|
4 | !function(F) {
|
5 |
|
6 | F.ttl = function(ms, onTimeout, scope) {
|
7 | var fn = this
|
8 | , tick = setTimeout(function() {
|
9 | ms = 0
|
10 | if (onTimeout) onTimeout()
|
11 | }, ms)
|
12 |
|
13 | return function() {
|
14 | clearTimeout(tick)
|
15 | if (ms) fn.apply(scope, arguments)
|
16 | }
|
17 | }
|
18 |
|
19 |
|
20 | F.once = function(ms, scope) {
|
21 | var tick, args
|
22 | , fn = this
|
23 | return function() {
|
24 | clearTimeout(tick)
|
25 | args = arguments
|
26 | tick = setTimeout(function() {
|
27 | fn.apply(scope, args)
|
28 | }, ms)
|
29 | }
|
30 | }
|
31 |
|
32 |
|
33 |
|
34 | F.rate = function(ms, last_call, _scope) {
|
35 | var tick, args
|
36 | , fn = this
|
37 | , scope = _scope
|
38 | , next = 0
|
39 | return function() {
|
40 | var now = Date.now()
|
41 | clearTimeout(tick)
|
42 | if (_scope === void 0) scope = this
|
43 | if (now >= next) {
|
44 | next = now + ms
|
45 | fn.apply(scope, arguments)
|
46 | } else if (last_call) {
|
47 | args = arguments
|
48 | tick = setTimeout(function() {
|
49 | fn.apply(scope, args)
|
50 | }, next - now)
|
51 | }
|
52 | }
|
53 | }
|
54 | }(Function.prototype)
|
55 |
|