Flash AS3ExternalInterface 调用 jQuery 文档内的函数已准备就绪
从 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当 Flash 调用 jsFunction 时,它尚未定义。您有一个竞争条件,其中
$(document).ready
在进行ExternalInterface 调用后触发,因此$(document).ready
中定义的任何内容都不会执行,因此在 Flash 进行调用时不可用。回应您的评论:
您需要准备好 Flash 和文档才能使其正常工作。我不确定初始化的顺序是否得到保证,因此我建议您从 Flash 调用一个已知函数来告诉 JS 它已准备就绪。也许是这样的:
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: