Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/70.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 将布尔值从true更改为false,然后使用jquery在if()中使用它_Javascript_Jquery_If Statement_Boolean_Var - Fatal编程技术网

Javascript 将布尔值从true更改为false,然后使用jquery在if()中使用它

Javascript 将布尔值从true更改为false,然后使用jquery在if()中使用它,javascript,jquery,if-statement,boolean,var,Javascript,Jquery,If Statement,Boolean,Var,我尝试了几种方法来实现从布尔语句到if语句的输出。不知怎的,我做错了。我可以更改布尔值和console.log,所以应该是正确的。然后我尝试在if语句中使用它,但不知何故它被忽略了,并且没有给出预期的结果。 这是我的代码: var heroVelX = game.currentHero.GetLinearVelocity().x; var heroVelY = game.currentHero.GetLinearVelocity().y; var speed = Math

我尝试了几种方法来实现从布尔语句到if语句的输出。不知怎的,我做错了。我可以更改布尔值和console.log,所以应该是正确的。然后我尝试在if语句中使用它,但不知何故它被忽略了,并且没有给出预期的结果。 这是我的代码:

    var heroVelX = game.currentHero.GetLinearVelocity().x;
    var heroVelY = game.currentHero.GetLinearVelocity().y;
    var speed = Math.sqrt(Math.pow(heroVelX, 2) + Math.pow(heroVelY, 2));
    var moveOn = "";

    function delay(){   
        moveOn = Boolean(speed<1.5);
        console.log("Speed = " + speed + "  " + moveOn);    
    };  

    setTimeout(delay, 3500);

    // These are the conditions I have tried using. I can see in console.log that the value is changed. But somehow it is ignored? All the other conditions are accepted in the if-statement.       
    // moveOn ===!false
    // moveOn == true

    if(!game.currentHero.IsAwake() || moveOn === !false || heroX<0 || heroX >game.currentLevel.foregroundImage.width ){
            // then delete the old hero
            box2d.world.DestroyBody(game.currentHero);
            game.currentHero = undefined;
            // and load next hero
            game.mode = "load-next-hero";
        }
var heroVelX=game.currentHero.GetLinearVelocity().x;
var herovly=game.currentHero.GetLinearVelocity().y;
var speed=Math.sqrt(Math.pow(heroVelX,2)+Math.pow(Herovly,2));
var moveOn=“”;
函数delay(){

moveOn=Boolean(速度你做错了几件事

首先,将变量定义为字符串是很可怕的,然后将其更改为布尔值

其次,将布尔值放入另一个日志中,并改用dir

if语句太混乱了

而且不需要强制转换为布尔值

看看这个:

var heroVelX = game.currentHero.GetLinearVelocity().x;
var heroVelY = game.currentHero.GetLinearVelocity().y;
var speed = Math.sqrt(Math.pow(heroVelX, 2) + Math.pow(heroVelY, 2));
var moveOn = false;

function delay(){   
    moveOn = (speed<1.5) ? true : false;
    console.log("Speed = " + speed);
    console.dir(moveOn);


};  

setTimeout(delay, 3500);

if(!game.currentHero.IsAwake() || moveOn || heroX<0 || heroX >game.currentLevel.foregroundImage.width ){
        // then delete the old hero
        box2d.world.DestroyBody(game.currentHero);
        game.currentHero = undefined;
        // and load next hero
        game.mode = "load-next-hero";
    }
var heroVelX=game.currentHero.GetLinearVelocity().x;
var herovly=game.currentHero.GetLinearVelocity().y;
var speed=Math.sqrt(Math.pow(heroVelX,2)+Math.pow(Herovly,2));
var-moveOn=false;
函数delay(){

moveOn=(速度
setTimeout(延迟,3500);
此行将运行,然后继续下一个(您的条件),其中
moveOn
仍为
延迟()
尚未执行。在moveOn发生更改之前,我如何停止程序?或者有其他解决方案吗?
setTimeout
将调用调度到
delay()
因此,如果您希望在延迟后运行程序,请将其放入
delay()中
我能不能设置
moveOn=false;
?我知道声明一个字符串然后将其更改为布尔值的意义..dir对我来说是新的,没有给我任何反馈?我如何改进if语句?我试图使用你的建议,但它不能解决我的问题?使用.dir而不是.log的想法是因为.log会将值转换为s字符串,布尔值的强制转换字符串是一个空字符串,您将看不到。.dir将向您显示变量的实际值。另请参阅我的代码。我删除了if语句中moveOn的所有运算符。在if语句中,布尔值本身足以为true/false。您现在有什么行为?我已删除了delay()函数,因为它会中断布尔值。它不会传递它。它不是最优的,但它可以。