AS3 Asynchronous ExternalInterface
The following example does exactly that. It provides a layer between your JS and your Flash for converting ExternalInterface.call methods to an asynchronous method.
The first part of this method is the AS3 class, AsyncExternalInterface:
This class acts like a Singleton where you get the instance via the CallStack method made available in window. This means that when you include the JS in the page, you don’t have to do any instantiating yourself, or pass any variable names back to Flash. It’ll figure all that out itself. Of course, if you’re using this with the AS3 class, you don’t need to know about any of that because it’s all done behind the scenes.
The class also has some cleanup methods so that while the stack is empty, it doesn’t waste any time polling an interval.
Finally, it’s time to put the files together into an HTML wrapper.
For our test, Flash is calling “testFunction” four times. The first three have sequential parameters, and the last has no parameters. If you have a FlashLog.txt file set up, you can also note the appearance of the traces vs the alerts. In the AsyncExample.as file, the traces come after the ExternalInterface calls. Were we using a normal ExternalInterface, you wouldn’t see any of those traces until after the alerts completed. Because of the AsyncExternalInterface solution, though, the traces will appear instantly, before the first alert has time to fire. The full example code can be found over on github. Enjoy!