简体   繁体   English

if 语句不停止执行

[英]if statement doesn't stop execution

It's been one or two weeks since I started learning to code and I am trying to create a "Press Key To Play" type thing.自从我开始学习编码以来已经有一两个星期了,我正在尝试创建一个“按键播放”类型的东西。 I've tried everything i know and still get the same results every single time.我已经尝试了我所知道的一切,但每次仍然得到相同的结果。

In sum, i have an if statement which checks if the game has already started if gameStart === false , then, if gameStart happens to be false, run a code to detect keypresses on the keyboard + run the game code.总之,我有一个 if 语句, if gameStart === false则检查游戏是否已经开始,然后,如果gameStart恰好为假,则运行代码以检测键盘上的按键 + 运行游戏代码。

 let gameStart = false; const runGame = () => { console.log(`game running`, { gameStart }) } if (gameStart === false) { $(document).keydown(function () { runGame(); gameStart = true; }); }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> click on me and then press any key

I've tried multiple variations, including else if , While Loops and changing the order of precedence.我尝试了多种变体,包括else if 、 While 循环和更改优先顺序。 The browser's dev tools even say that gameStart is indeed true, but the code runs regardless.浏览器的开发工具甚至说gameStart确实是真的,但代码无论如何都会运行。

Either the code is stuck and keeps detecting keypresses or it doesn't do it at all.要么代码被卡住并不断检测按键,要么根本不这样做。

Please, I know I might be doing something fairly dumb to get the same results all the time, but i need help lmao.拜托,我知道我可能一直在做一些相当愚蠢的事情来获得相同的结果,但我需要帮助 lmao。

  • Me: Hey Joe, did we start the game yet?我:嘿乔,我们开始游戏了吗?
  • Joe: No, we haven't.乔:不,我们没有。
  • Me: Okay then, if so, when you hear a whistle, yell to start the game.我:好吧,如果是这样,当你听到哨声时,大喊开始游戏。
  • Joe: Okay, boss.乔:好的,老板。
  • Ref: [whistles]参考:[口哨]
  • Joe: START THE GAME!乔:开始游戏!
  • Fan: [whistles]粉丝:[吹口哨]
  • Joe: START THE GAME!乔:开始游戏!
  • Kettle: [whistles]水壶:[口哨]
  • Joe: START THE GAME!乔:开始游戏!
  • Me: Joe, the game is already running, why are you yelling?我:乔,比赛已经开始了,你喊什么?
  • Joe: You didn't tell me to stop yelling when I hear a whistle.乔:当我听到哨声时,你没有告诉我停止大喊大叫。
  • Me: I asked you if the game started!我:我问你游戏开始了吗!
  • Joe: Yeah, but that was then .乔:是的,但那是那时

In order to do what you want, there are two ways to do this:为了做你想做的事,有两种方法可以做到这一点:

  • Tell Joe to check if the game is running every time he hears the whistle, and not mindlessly yell on reflex.告诉乔每次听到哨声时检查游戏是否正在运行,而不是反射性地大喊大叫。

     let gameStarted = false; $(document).keydown(function () { if (;gameStarted) { runGame(); gameStarted = true; } });
  • Tell Joe to stop listening for a whistle once he starts the game.告诉乔一旦开始比赛就不要再听口哨了。

     function gameRunner() { runGame(); $(document).off('keydown', gameRunner); } $(document).keydown(gameRunner);
  • You can also tell Joe to only listen to the first whistle, but this is the same as telling Joe to stop listening after the first whistle (ie jQuery's one is doing what the previous example showed, under the hood):你也可以告诉 Joe 只听第一个哨声,但这与告诉 Joe 在第一个哨声后停止听是一样的(即 jQuery one例子正在做前面的例子,在引擎盖下):

     $(document).one('keydown', runGame);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM