简体   繁体   English

布尔值在Flash中为true,但在Javascript中为false

[英]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.

相关问题 JavaScript布尔值true变为false,false则不变为true - Javascript boolean true turns false, false does not turn true Javascript-将1/0和'true'/'false'和true / false转换为布尔值的好方法 - Javascript - Nice way to convert 1/0 & 'true'/'false' & true/false to boolean 如何在javascript中将布尔值true或false转换为1或0 - How to convert boolean true or false into 1 Or 0 in javascript Javascript 检查字符串是否为真或假并转换为 Boolean - Javascript check if string if true or false and convert to Boolean 是否可以在 javascript 中强制 boolean true 等于 false? - Is it possible to force the boolean true to equal false in javascript? javascript中的Boolean对象为“false”参数返回true - Boolean object in javascript returns true for “false” parameter 在JavaScript函数中返回布尔值true或false - Return boolean true or false in a JavaScript function 检查Typescript / Javascript中的布尔值,分别与“ true”,“ false”和“ undefined”有关 - Checking for Boolean Values in Typescript/Javascript Pertaining to “true”, “false”, and “undefined” 在布尔TypeScript / JavaScript方法中返回true / false的正确方法? - Right way to return true/false in boolean TypeScript/JavaScript method? 在 JavaScript 中,如果 Boolean 为真/假,我似乎无法做出 if 语句 - In JavaScript I can't seem to make an if statement for if an Boolean is True/False
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM