Javascript 计数器未启动的jquery while循环

Javascript 计数器未启动的jquery while循环,javascript,jquery,html,Javascript,Jquery,Html,我试图在JQuery中创建一个while循环 $(document).ready(function () { //startgame(); var game = { started: false, jumper: 0, //player jump active or not seconds: 0 } $('body').keyup(function (space) { if (space.keyCode == 32) { //32 = spatie

我试图在JQuery中创建一个while循环

$(document).ready(function () {
//startgame();
var game = {
    started: false,
    jumper: 0, //player jump active or not
    seconds: 0
}

$('body').keyup(function (space) {
    if (space.keyCode == 32) { //32 = spatie

        if (game.started != true) { //set game to started
            game.started = true;
            console.log("game started? " + game.started);
        };

        if (game.jumper == 0) { //start jump
            game.jumper = 1; //jump is in action
            $("#player").animate({ //animate to air
                top: "-=75"
            }, 250, function () {
                $(this).animate({ //return to ground
                    top: "+=75"
                }, 500, function () {
                    game.jumper = 0;
                });
            });
        }
    }
}); //end space

//not even running this code
while (game.started == "true") {
    var squaretimer = Math.random(1000, 3000);
    console.log(squaretimer);
    $("#border").append("<div class='driehoek'></div>");
}
});
$(文档).ready(函数(){
//startgame();
var博弈={
开始:错,
跳线:0,//玩家是否跳转激活
秒:0
}
$('body').keyup(函数(空格){
如果(space.keyCode==32){//32=spatie
如果(game.started!=true){//将游戏设置为已启动
game.start=true;
console.log(“游戏开始了?”+游戏开始了”);
};
如果(game.jumper==0){//开始跳转
game.jumper=1;//跳跃正在进行
$(“#播放器”).animate({//animate to air
顶部:“-=75”
},250,函数(){
$(此)。设置动画({//返回地面
顶部:“+=75”
},500,函数(){
game.jumper=0;
});
});
}
}
});//结束空间
//甚至不运行这个代码
while(game.start==“true”){
var squaretimer=数学随机(10003000);
console.log(squaretimer);
$(“#边框”)。追加(“”);
}
});
控制台中没有错误。 因此,我们的想法是,代码最终会生成(在1/3秒内随机生成一个新的div)
但是整个while循环没有运行。我做错什么了吗?

如果要将布尔值与字符串进行比较,应该执行以下操作:

while(game.started){
....
}
更新: 此外,在按start开始游戏之前,会立即调用循环。你需要马上执行它。你可以:

将循环放入函数中:

function startLoop() {
  while (game.started) {
      var squaretimer = Math.random(1000, 3000);
      console.log(squaretimer);
      $("#border").append("<div class='driehoek'></div>");
  }
}

希望这有帮助。

试试这样的方法

$(document).ready(function () {
//startgame();
var game = {
    started: false,
    jumper: 0, //player jump active or not
    seconds: 0
}

$('body').keyup(function (space) {
    if (space.keyCode == 32) { //32 = spatie

        if (game.started != true) { //set game to started
            game.started = true;
            console.log("game started? " + game.started);
        };

        if (game.jumper == 0) { //start jump
            game.jumper = 1; //jump is in action
            $("#player").animate({ //animate to air
                top: "-=75"
            }, 250, function () {
                $(this).animate({ //return to ground
                    top: "+=75"
                }, 500, function () {
                    game.jumper = 0;
                });
            });
        }
    }
}); //end space

//not even running this code
while (game.started) {
    var squaretimer = Math.random(1000, 3000);
    console.log(squaretimer);
    $("#border").append("<div class='driehoek'></div>");
}
});
$(文档).ready(函数(){
//startgame();
var博弈={
开始:错,
跳线:0,//玩家是否跳转激活
秒:0
}
$('body').keyup(函数(空格){
如果(space.keyCode==32){//32=spatie
如果(game.started!=true){//将游戏设置为已启动
game.start=true;
console.log(“游戏开始了?”+游戏开始了”);
};
如果(game.jumper==0){//开始跳转
game.jumper=1;//跳跃正在进行
$(“#播放器”).animate({//animate to air
顶部:“-=75”
},250,函数(){
$(此)。设置动画({//返回地面
顶部:“+=75”
},500,函数(){
game.jumper=0;
});
});
}
}
});//结束空间
//甚至不运行这个代码
while(游戏开始){
var squaretimer=数学随机(10003000);
console.log(squaretimer);
$(“#边框”)。追加(“”);
}
});
应该是

while (game.started == true) 

加载DOM后立即进入和退出while循环。可能是因为您正在将
布尔值
字符串
进行比较?
游戏。Start
仅在按空格键时设置为true,因此当它点击while循环时,您没有按空格键,因此它为false-立即退出。@freedomn-m这两种方式都是错误的。循环将不会运行,因为DOM readystill上的条件为false。将不运行意味着“game.started”始终为false…调试您的代码。因为当循环条件发生时。。。尝试添加game.started=true并在循环工作时进行检查…请不要发布裸代码,也请提供代码正在执行的解释。
$(body).keyup(function(space) {
    //your code
    someFunction(game.started);
});

function someFunction(a) {
    this.a = a;
    while (this.a) {
        // your code again
    }
}
// The purpose of calling function is that it binds in keyup event
while (game.started == "true") 
while (game.started == true)