Denoteone
Denoteone

Reputation: 4055

Call a Flash AS3 function every 20 seconds

How do you set a function to increment in AS3 Flash. I am trying to start the incremented function when my video starts and then run the same function every 20 seconds until the video stops.

something like:

    my_player.addEventListener(VideoEvent.COMPLETE, completePlay);
    my_player.addEventListener(VideoEvent.PLAYING_STATE_ENTERED, startPlay);

   function startPlay(){
       startInc();
       //OTHER items are started and set within this function that do not have to do with the incremented function.
    }


   function completePlay(){
       //This is where the startInc is stopped but not removed since it will be used again.

    }


     function startInc(){
          //This function should run every 20 seconds.
     }

Upvotes: 1

Views: 6678

Answers (2)

pho
pho

Reputation: 25489

var i:uint;

function startPlay(){
    i=setInterval(startInc, 20000);
}


function completePlay(){
    clearInterval(i);
}


function startInc(){
     //This function will run every 20 seconds.
}

Upvotes: 1

Jason Sturges
Jason Sturges

Reputation: 15955

User a timer around your player's VideoEvents.

package
{
    import flash.display.Sprite;
    import flash.events.TimerEvent;
    import flash.events.VideoEvent;
    import flash.utils.Timer;

    public class IncrementTimer extends Sprite
    {

        private var my_player:*;

        private var timer:Timer;

        public function IncrementTimer()
        {
            my_player.addEventListener(VideoEvent.COMPLETE, completePlay);
            my_player.addEventListener(VideoEvent.PLAYING_STATE_ENTERED, startPlay);
        }

        protected function startPlay(event:VideoEvent)
        {
            timer = new Timer(20000);
            timer.addEventListener(TimerEvent.TIMER, startInc);
            timer.start();
        }

        protected function completePlay(event:VideoEvent)
        {
            timer.reset();
        }

        protected function startInc(event:TimerEvent)
        {
            // called every 20-seconds
        }

    }
}

Upvotes: 5

Related Questions