Flash AS3 ExternalInterface call to function inside jQuery document ready

From a button in Flash I just want to call a function written in jQuery.When I place the function outside jQuery's $(document).ready it works fine: *btw I use SWFObject to embed Flash.

AS3:

import flash.external.ExternalInterface;
function test_fnc(event:Event):void {
    ExternalInterface.call("jsFunction", "hello world");
}
test_mc.addEventListener("click", test_fnc);

JS:

<script type="text/javascript">     
    function jsFunction(words) {
        alert(words); // "hello world";
    }
    $(document).ready(function() {
        // not from here
    });
</script>

Answers


At the time the Flash makes a call to jsFunction it is not defined. You have a race condition where $(document).ready is firing after the ExternalInterface call is made, so anything defined within $(document).ready would not yet have executed, and therefore be unavailable at the time Flash makes the call.

In response to your comment:

You need both Flash to be ready and the document to be ready for this to work. I'm not sure an order of initialization is guaranteed, so I'd advise you to call a known function from Flash that tells JS that it is ready. Perhaps something like this:

var waitingForItems=2;
function itemReady()
{
    //called from both Flash and $(document).ready
    --waitingForItems;
    if(waitingForItems==0)
    {
        //create your array
        //send to Flash by calling Flash rather having Flash call JS
    }
}
$(document).ready(function(){
    itemReady();
});

Need Your Help

Custom user controls with the same method, calling them the same way

c# reflection user-controls

So I have made a bunch of user controls in my project which integrate into a system automatically that tracks certain things about them. But I'm unsure on how to do a certain part without it looking

How to force Android to re-index all the photos on the phone?

android file photo android-4.2-jelly-bean

I noticed something odd with the way Galaxy Nexus handles photos and file directories. I create them but Android (4.2.2) does not know they exist until you reboot. Well at least on my Nexus 4. I am

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.