-
timer removal
I have timer that executes 5 times. I have an event listener set to update a function but what I'm trying to find out is does the timer get trashed when it is finished or does it stay in the memory because it has a private referance to it in the class.
Also how to clean up the listener after it has executed 5 times?
I could set a count in the function that is been updated by the listener but that just seems like more work. IS there a simple way to clean all this up when finished.
Code:
class.........
private var timer : Timer;
private function setTimer() ; void {
timer = new Timer(400, 5);
timer.addEventListener(TimerEvent.TIMER, onUpdate );
timer.start();
}
private function onUpdate() : void {
// update code in here....
}
-
Timer already has counters built in so you don't need to worry about that, and it will give you a complete event after it has fired for the last time. Just listen to the complete event, use the event object's target (which is a callback pointer to the timer) and clear the listeners that way.
PHP Code:
private function setTimer() ; void {
// declare the var here so it doesn't need to be deleted later
var timer:Timer = new Timer(400, 5);
timer.addEventListener(TimerEvent.TIMER, onUpdate);
timer.addEventListener(TimerEvent.TIMER_COMPLETE, onComplete);
timer.start();
}
private function onUpdate(e:TimerEvent) : void {
// update code in here....
trace('Running: ' + e.target.running, '\t', 'Tick ' + e.target.currentCount + ' of ' + e.target.repeatCount);
}
private function onComplete(e:TimerEvent):void{
// remove event listeners on complete - nothing else holds the timer in memory
e.target.removeEventListener(TimerEvent.TIMER, onUpdate);
e.target.removeEventListener(TimerEvent.TIMER_COMPLETE, onComplete);
}
-
Oh that's great I was hoping there was something built in like this. Excellent thanks for the info.
-
It's all explained in the docs
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|