9 Replies Latest reply on Sep 16, 2011 5:07 AM by Claudiu Ursica

    Set a delay for a for loop!

    hincauti

      Hi there,

      I'm working on a flex mobile application now and I'm stucked with an issue. I need to pause or delay a for loop and I was'nt able to achieve it in any way.

       

      My code:

       

      public function takePicture():void

       

                  {

       

                     

       

                              for (var i:int = i; i<50; i++)

       

                              {
        • 1. Re: Set a delay for a for loop!
          hincauti Level 1

          Hi there,

          I'm working on a  flex mobile application now and I'm stucked with an issue. I need to  pause or delay a for loop and I was'nt able to achieve it in any way.

           

          My code:

           

          public function takePicture():void

           

                      {
                                  for (var i:int = 0; i<50; i++)

                                 {

                                       lbl.text = String(i);
                                       // I need a delay here
                                  }
                      }

          <s:Button id="trigger" y="10" label="Take a picture!" horizontalCenter="130" click="takePicture()"/>
          <s:Label id="lbl" x="10" y="83" width="155" fontFamily="_typewriter" fontSize="16"/>

          When I click the "trigger" button, the "lbl" label displays the last loop "49" I understand that the loop goes too fast and that's why I see only the last one. Ho to put a delay after each loop in order to run it twice per second.

          Thank You!
          • 2. Re: Set a delay for a for loop!
            Claudiu Ursica Level 4

            Loops are synchronous.

            What you can do is exit and then resume after a delay. Upon exiting save the exit index and then you will resume from that index on. The resume can be a timer or aother delay mechanism.

             

             

            C

            1 person found this helpful
            • 3. Re: Set a delay for a for loop!
              hincauti Level 1

              Could you please write an example of code how it can be achieved?  I'm little confused about how exactly the loop resume works.

               

              Thank You.

              • 4. Re: Set a delay for a for loop!
                Claudiu Ursica Level 4

                What are you trying to achieve to display a delay before taking picture or what?

                It might be that the loop is not the best way to do that ... Provide some more details pls.

                 

                C

                1 person found this helpful
                • 5. Re: Set a delay for a for loop!
                  hincauti Level 1

                  I'm working on a flash builder mobile project. What my app does is: It takes 50 snapshots from a camera and then returns the RGB value of a specified pixel. I need a for loop and a delay to take those 50 snapshots at an exact interval (ex. 4 snapshots per second).

                   

                  Thank You!

                  • 6. Re: Set a delay for a for loop!
                    kokorito Level 4

                    cant you use a timer instead of a loop

                    • 7. Re: Set a delay for a for loop!
                      hincauti Level 1

                      Yeah, you're right, this is the right method, just founded.

                       

                      Thank you both!

                      • 8. Re: Set a delay for a for loop!
                        Claudiu Ursica Level 4

                        Why don't you setup a timer which fire every x miliseconds (x = 1000/4), take the snapshot and register the value. The timer will fire 50 times and you don't need any delay because the snapshot are being taken at the proper interval.

                         

                        C

                        • 9. Re: Set a delay for a for loop!
                          hincauti Level 1

                          Thank You Claudiu!

                           

                          you are right about the timer class. I've looked in some actionscript 2.0 documentation and that's why I got stucked with setInterval function. Didn't know about the Timer clas introduced in the as 3.0.

                           

                          PS. Looking at you first and last name I'm assuming You're from Romania, so....Multumesc mult pt ajutor si multa bafta in continuare!

                           

                          Salut!