[英]Boolean is true in Flash, but false in Javascript
I have this function in Flash: 我在Flash中具有以下功能:
public function checkFile(file:String):Boolean{
var b:Boolean;
var responder:Responder = new Responder(function(reply:Boolean):void{
b=reply;
msg("vid_"+file+".flv "+ "exists? " + reply);
setState("ready");
status = "ready";
},
function(res:Object):void{
trace("checkFile call failed");
});
mync.call("checkFile",responder,"vid_"+file);
return b;
}
I can confirm that the reply
variable is true, but return b
ends up false: 我可以确认
reply
变量为true,但是return b
最终为false:
This is the javascript I use to call the flash function: 这是我用来调用Flash函数的JavaScript:
function checkFile(){
alert(thisMovie("vidRecorder").checkFile(currentVid));
}
And that opens up a message box saying false
, while the flash file displays true
然后打开一个消息框,显示
false
,而flash文件显示true
What's going on? 这是怎么回事? How can I fix it so that my function returns the same value as
reply
? 我该如何解决它,以便我的函数返回与
reply
相同的值?
This happens because the anonymous function in your responder is executed asynchronously. 发生这种情况是因为响应器中的匿名函数是异步执行的。
When your checkFile()
function returns the value of b
is still false
. 当您的
checkFile()
函数返回b
时,其值仍为false
。 Eventually, your anonymous function is executed and b
gets set to true
... but it's too late in this case, as your checkFile()
function has already returned false
. 最终,您的匿名函数被执行并且
b
设置为true
……但是在这种情况下为时已晚,因为您的checkFile()
函数已经返回了false
。
To work around this, you might consider doing this in a slightly different fashion: 要变通解决此问题,您可以考虑以略有不同的方式执行此操作:
When your anonymous function is executed, have it call out to javascript with the asynchronous response. 当您执行匿名函数时,请使用异步响应将其调出至javascript。 Something like this:
像这样:
public function checkFile(file:String):void {
var responder:Responder = new Responder(function(reply:Boolean):void{
msg("vid_"+file+".flv "+ "exists? " + reply);
setState("ready");
status = "ready";
ExternalInterface.call('someJavascriptFunction', reply);
},
function(res:Object):void{
trace("checkFile call failed");
});
mync.call("checkFile",responder,"vid_"+file);
// note we no longer return a value here
}
In the above code, we call a new Javascript function and provide it the result of the asynchronous operation. 在上面的代码中,我们调用了一个新的Javascript函数,并为其提供了异步操作的结果。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.