1
10
11 import com.sekati.core.CoreObject;
12
13
21 class com.sekati.time.Throttle extends CoreObject {
22
23 private var _fn:Function;
24 private var _delay:Number;
25 private var _finalInt:Number = -1;
26 private var _delayInt:Number = -1;
27 private var _isThrottled:Boolean = false;
28 private var _finalCallPending:Boolean = false;
29
30
35 public function Throttle(proxyFunc:Function, msDelay:Number) {
36 super( );
37 _fn = proxyFunc;
38 _delay = msDelay;
39 }
40
41
44 public function trigger():Void {
45 clearInterval( _finalInt );
46 if(_isThrottled) {
47
48 _finalCallPending = true;
49 return;
50 } else {
51 _doFunctionCall( );
52 _finalCallPending = false;
53 _isThrottled = true;
54 clearInterval( _delayInt );
55 _delayInt = setInterval( this, "clearThrottle", _delay );
56 }
57 }
58
59
62 public function clearThrottle():Void {
63 clearInterval( _delayInt );
64 _isThrottled = false;
65 if(_finalCallPending) {
66
67 _doFunctionCall( );
68 }
69 }
70
71 private function _doFunctionCall():Void {
72
73 _finalCallPending = false;
74 _fn.call( null );
75 }
76
77
80 public function destroy():Void {
81 clearThrottle( );
82 super.destroy( );
83 }
84 }