简体   繁体   English

Flash AS3 ExternalInterface调用可在jQuery文档中准备好功能

[英]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. 从Flash中的按钮,我只想调用用jQuery编写的函数。
When I place the function outside jQuery's $(document).ready it works fine: 当我将函数放在jQuery的$(document).ready之外时,它可以正常工作:
*btw I use SWFObject to embed Flash. *顺便说一句,我使用SWFObject嵌入Flash。

AS3: AS3:

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

JS: JS:

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

At the time the Flash makes a call to jsFunction it is not defined. 在Flash调用jsFunction它尚未定义。 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. 您有一个竞争条件,其中在进行ExternalInterface调用后会触发$(document).ready ,因此$(document).ready定义的任何内容都尚未执行,因此在Flash进行调用时不可用。

In response to your comment: 针对您的评论:

You need both Flash to be ready and the document to be ready for this to work. 您既需要准备好Flash,也需要准备好文档才能使其正常工作。 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. 我不确定初始化顺序是否得到保证,因此建议您从Flash调用一个已知函数,以告知JS它已准备就绪。 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();
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM