7 Replies Latest reply on Oct 29, 2007 5:08 PM by EnterFrame

    Is it posible?

    artishox
      Is it possible to make delay inside function?
      I need function that starts then it stops for a while and then continues.
      trick is - i cannt split this function in two and use setInterval and it must be in one frame.
        • 1. Re: Is it posible?
          kglad Adobe Community Professional & MVP
          not unless you have a function within a function.

          what limitation(s), do you think, is making this difficult?
          • 2. Re: Is it posible?
            fermp Level 1
            You can probably do this by making the function only run if a certain condition is met. Set a variable like "functionRun" and make it either true or false depending on when you want to start or stop (delay) your setInterval script.
            • 3. Re: Is it posible?
              artishox Level 1
              I need function that loads variables from file and returns them as an array but usually function ends before variables are loaded and returns empty array, so i need that this function waits while variables is loaded and then returns an array thats why i cannot split function in two or make function inside function because i invoke one function that needs to return array but if this function will invoke another function witch will return arrays then the firs function will end but will not return any array. thats the basic problem
              • 4. Re: Is it posible?
                kglad Adobe Community Professional & MVP
                don't use a return value in the function that executes asynchronously if it doesn't fit your needs.
                • 5. Re: Is it posible?
                  artishox Level 1
                  but if i use it in function thats executes asynchronously then i get error that function that was executed fitst does not contain return!
                  becose it is definet like this

                  var aArray:Array = getMyArray();

                  function getMayArrau():Array {
                  some code
                  }

                  so i need return statement in getMyArray();
                  • 6. Re: Is it posible?
                    kglad Adobe Community Professional & MVP
                    you've locked yourself into thinking of one way to solve a problem (that doesn't work) and you're missing obvious alternative solutions (that do work). rethink your approach.

                    for example, you can have your onLoad or onData function call another function and pass that array as a parameter or just use that array if you don't make it local to your onLoad/onData function.
                    • 7. Re: Is it posible?
                      EnterFrame
                      I have encountered this issue when I load images from disk. In a for/foreach loop I add the images to the stage, this is a very quick operation and the loop finishes long before the images are actually loaded, this then causes a race condition.

                      What I have done to solve this is to put a counter int the first loop, counting the number of images I add to the stage (or loading the variables). I also register an event handler for each of the images loads (the Async part). I also have a class variable that increments inside this event handler. When the imageCount and the runningCount is the same, then you know that you have finished loading all of your images/variable, or if the counter in the first loop, matches the counter variable in the event handler then you know you have reached the end.