A Flash Developer Resource Site

Results 1 to 4 of 4

Thread: timer removal

  1. #1
    Senior Member
    Join Date
    Apr 2001
    Posts
    996

    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....
        }

  2. #2
    Ө_ө sleepy mod
    Join Date
    Mar 2003
    Location
    Oregon, USA
    Posts
    2,441
    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(4005);

        
    timer.addEventListener(TimerEvent.TIMERonUpdate);
        
    timer.addEventListener(TimerEvent.TIMER_COMPLETEonComplete);
        
    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.TIMERonUpdate);
        
    e.target.removeEventListener(TimerEvent.TIMER_COMPLETEonComplete);


  3. #3
    Senior Member
    Join Date
    Apr 2001
    Posts
    996
    Oh that's great I was hoping there was something built in like this. Excellent thanks for the info.

  4. #4
    Ө_ө sleepy mod
    Join Date
    Mar 2003
    Location
    Oregon, USA
    Posts
    2,441
    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
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center