3 Replies Latest reply on Jan 29, 2010 12:41 PM by jimmyoneshot

    Looping/Repeating a Function a Certain amount of times

    jimmyoneshot Level 1

      Is it possible to make a function loop/repeatedly acivate a certain number of times? For example if I have a function that activates upon creation complete of my application is there a way to make it keep activating a certain number of times or perhaps permanently?

       

      So if I had the following 'load function I've made in my creationComplete property of my application tag how can I make it loop several times or indeed permanently:-

       

      creationComplete="load()"

       

      The reason I need this is the load function takes screenshots of html components and produces them as images in my app but the html containers take a while to load their content so it takes a while for this to happen sometimes so obviously I need this function to keep repeating for a while until their content is loaded.

        • 1. Re: Looping/Repeating a Function a Certain amount of times
          Sebastien V. Level 3

          A simple way is to use a for loop and call your function any number of times. You can use a while(1>0) if you'd like it to run constantly.

           

          BUT

           

          Those 2 solutions are ugly and you shouldn't use them. Never. And neither should anybody.

           

          Instead if you'd just like to wait a certain amount of time before taking your screenshot, use the setTimeOut method:

           

          import flash.utils.setTimeout;
          
          public function load():void {
               setTimeOut( takeScreenshot , 1000 );
          }
          
          private function takeScreenshot():void {
               // do whatever you need to take your screenshot (content of your previous load method)
          }
          

           

          Hope this helps.

           

           

          EDIT:

          Oops, just checked out and Adobe recommands not to use setTimeOut any more with AS3, instead you should use the Timer class:

           

          import flash.utils.Timer;
          
          public function load():void {
               var timer:Timer = new Timer( 60000 , 1 ); // will wait 1x60000 ms = 1 min
               timer.addEventListener( TimerEvent.TIMER_COMPLETE , takeScreenshot );
               timer.start();
          }
          private function takeScreenshot( event:TimerEvent ):void {
               // do whatever you need to take your screenshot (content of your previous load method)
          }
          
          • 2. Re: Looping/Repeating a Function a Certain amount of times
            Jason Szeto Level 3

            You should be able to listen to the complete or htmlRender events of your mx:HTML controls. Take a look at the ASDoc for explanations and usage.

             

            Jason

            1 person found this helpful
            • 3. Re: Looping/Repeating a Function a Certain amount of times
              jimmyoneshot Level 1

              Thanks a lot NoNick4Me. The timeout method worked fine actually.

               

              Thanks for your help too Jason, I'll check that out.