UNPKG

990 BJavaScriptView Raw
1
2
3
4!function(F) {
5 // Time to live - Run *onTimeout* if Function not called on time
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 // Run Function one time after last call
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 // Maximum call rate for Function
33 F.rate = function(ms, last_call, scope) {
34 var tick, args
35 , fn = this
36 , next = 0
37 return function() {
38 var now = +new Date()
39 clearTimeout(tick)
40 if (now > next) {
41 next = now + ms
42 fn.apply(null, arguments)
43 } else if (last_call) {
44 args = arguments
45 tick = setTimeout(function() {
46 fn.apply(scope, args)
47 }, next - now)
48 }
49 }
50 }
51}(Function.prototype)
52