5 Replies Latest reply on Nov 8, 2006 1:03 AM by fly42

    Help! - Variables (Edit: Wait Function??)

    QuickStick
      Hi all,

      Is there any way Flex can start a function, wait for something like data to load before returning the function's value.

      In my instance, I'm loading XML with a HTTPService and I'm sure it's returning the value of the function before waiting for the HTTPService to complete as per the eventListener.

      EG:

      public function getData():String {

      //Load HTTPService data...

      //Data complete eventListener...

      //Wait until data has been processed...

      return data;

      }

      Any and all help appreciated...

      Thanks.
      Oz
        • 1. Re: Help! - Variables (Edit: Wait Function??)
          fly42
          perhaps posting actual code might help?
          • 2. Re: Help! - Variables (Edit: Wait Function??)
            ntsiii Level 3
            No. There is NO WAY to do that. The Flash Player is essentially single threaded and there is no way to block execution of that thread while some other process does something.

            You MUST use either a resuilt event handler function, or bind. I advise the former, because it is easier to debug.

            On a related note: The Alert box and the "modal" pop-ups are NOT truly modal. They do not stop the execution of code. To use them for something like a confirm dialog, again yo must use events.

            Tracy
            • 3. Re: Help! - Variables (Edit: Wait Function??)
              ntsiii Level 3
              You are on the right track using the event listener. What is not working?
              Tracy
              • 4. Re: Help! - Variables (Edit: Wait Function??)
                QuickStick Level 1
                Thanks Tracy,

                I think I've finally sorted out how I need to achieve it and I know where it's going wrong.

                I'm trying to load XML data into a function called by a variable, thus the return aspect.

                But in the time it takes for the data to load (even using an eventListener) the function has returned it's value, which is none.

                What I think I need to do is something like this:

                Function {

                load xmlData using HTTPService

                on load complete eventListener {

                transfer loaded xmlData into varXmlData variable

                }

                Wait until varXmlData has been filled

                return varXmlData;

                }

                Hopefully, that makes sense, my code's a bit too long to post.

                So I can either bodgy a:

                While(varXmlData == "") {

                Do something pointless

                }

                Or, some programming languages have a Wait(interval) or Sleep(interval) function so they wait for the required interval before continuing.

                However, as far as I can tell these languages all seem to be Multi-Threaded, so it looks like I have to go the bodgy way.

                I'll give it a shot and let you know.

                Thanks for you help.

                • 5. Re: Help! - Variables (Edit: Wait Function??)
                  fly42 Level 1
                  I would recommend using a real (not anonymous/inline) function as eventhandler and handle the xml data from there. Basically you don't use the data within the function that now calls your LoadXml method but in the eventhandler. You LoadXml method "returns" void. This is a fairly common way of handling async method calls.

                  Depending on your app, you can show a loading page until the xml has finished loading or do it at app start up or whatever.

                  I strongly advice against using a busy spn.

                  quote: