9 Replies Latest reply on Jul 23, 2007 2:34 PM by Michael Kraft

    let a funtion wait till another ist complete

    Michael Kraft
      Hi all,

      I have a funtion, that calls a part of a viewStack. On this viewStack is another function , that is called with the creationComplete tag.

      If I use the 1st function after I called the viewStack manually - so the creationComplete function did his job my function works well.

      If I use the 1st function before that I got an error. This makes sense, because the 2nd function need to be completed before the 1st function can get to his goal.

      So, is there a way to tell the first function to check if the 2nd function has run and if not trigger it and wait for it?

      Thanks - Michael


        • 1. Re: let a funtion wait till another ist complete
          Bob Saggot Level 1
          why don't you just create a variable

          var ifFunction1ran:Boolean = false;

          //then function 1 will be

          private function1():void
          {
          run whatever you want
          //last line in code
          ifFunction1ran = true;
          }

          //then an if statement saying if function one has been run or not in a function that you add to the place where function2() currently was ran.

          private should-I-run-function2():void
          {
          if(ifFunction1ran == true)
          {
          function2(); //run function 2
          }
          }

          //then of course whatever function 2 is

          private function2():void
          {
          blah
          }

          might be ugly code, but its something.
          • 2. Re: let a funtion wait till another ist complete
            Or another alternative is just execute the second function instead of creating a variable. Execute the second function in the result handler.

            I have faced a similar issue when executing a function using remote object. Since the remote object call is async, I had to wait for the results to come back before I could do anything .

            I have something like this which works for me.

            private function_1():void {
            remoteObject.doSomething(args);
            }

            // result handler for remote object
            private function resultHandler(event:ResultEvent):void {
            var message:String = event.result as String;
            function_2();
            }

            private function_2():void {
            }

            One less variable to maintain ;-)
            • 3. Re: let a funtion wait till another ist complete
              Level 7
              Or another alternative is just execute the second function instead of creating
              a variable. Execute the second function in the result handler.

              I have faced a similar issue when executing a function using remote object.
              Since the remote object call is async, I had to wait for the results to come
              back before I could do anything .

              I have something like this which works for me.

              private function_1():void {
              remoteObject.doSomething(args);
              }

              // result handler for remote object
              private function resultHandler(event:ResultEvent):void {
              var message:String = event.result as String;
              function_2();
              }

              private function_2():void {
              }

              One less variable to maintain ;-)


              • 4. Re: let a funtion wait till another ist complete
                Michael Kraft Level 1
                Gnilt, the problem for me in your solution is, that I have to run function2() only once. So I guess Bobs answer will bring me forward.

                Bob, what will happen in your solution if function1ran == false?
                first run the function I need and then private should-I-run-function2() again? Do they work one after another like this:
                {
                if(ifFunction1ran == false)
                {
                function1(); // first run this
                function2(); //run function 2
                }
                }

                I hope my questions aren't that stupid, but I'm still learning.

                Thanks to both of you!
                • 5. Re: let a funtion wait till another ist complete
                  Bob Saggot Level 1
                  in my solution, function 2 will ONLY run if function 1 was ever ran.

                  you never want to run function 2 directly, instead replace it with should-I-run-function2()

                  example:
                  say you have button 1 that runs function1 (executes code)
                  button 2, runs should I run function 2()

                  if they never click on button1, button2 will just do nothing if they click on it.
                  • 6. Re: let a funtion wait till another ist complete
                    connectricity
                    You could just create an event listener, and then fire off a custom event on the creation complete of your component in the view stack.
                    • 7. Re: let a funtion wait till another ist complete
                      Michael Kraft Level 1
                      I finally solve the problem with 'creationPolicy="all" '. That does the init() on Application load. thx. m.
                      • 8. Re: let a funtion wait till another ist complete
                        Level 1
                        Just be careful using creationPolicy="all" as it tends to slow things down. I found a blog discussing this issue

                        http://weblogs.macromedia.com/pent/archives/2005/09/creationpolicy.cfm#comments
                        • 9. Re: let a funtion wait till another ist complete
                          Michael Kraft Level 1
                          You are right gnilt. I now solved the issue this way. I have for the Stack with the initFunction a loaderState. So I switch over to the stackChild on Complete and changed the progressBarText to "Application is initializing...". In the beginning of the initFunction(called, when content is loaded) I switch back to state"" and select the mainStackChild. The rest of the function works well in the background.
                          Because I use a customPreloader(a fading png) there is only one Progressbar at all.

                          A Fake, I know, but who cares ;-)