Flash AS3ExternalInterface 调用 jQuery 文档内的函数已准备就绪

发布于 2024-08-24 05:03:14 字数 594 浏览 6 评论 0原文

从 Flash 中的按钮,我只想调用用 jQuery 编写的函数。
当我将该函数放在 jQuery 的 $(document).ready 之外时,它工作正常:
*顺便说一句,我使用 SWFObject 来嵌入 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>

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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梨涡少年 2024-08-31 05:03:14

当 Flash 调用 jsFunction 时,它尚未定义。您有一个竞争条件,其中 $(document).ready 在进行ExternalInterface 调用后触发,因此 $(document).ready 中定义的任何内容都不会执行,因此在 Flash 进行调用时不可用。

回应您的评论:

您需要准备好 Flash 和文档才能使其正常工作。我不确定初始化的顺序是否得到保证,因此我建议您从 Flash 调用一个已知函数来告诉 JS 它已准备就绪。也许是这样的:

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();
});

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();
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文