简体   繁体   English

一旦满足条件,如何防止 IF 语句运行

[英]How can I prevent IF statement from running once the condition is met

I'm making a game where players have to input numbers to guess the population of a country, but I'm having issues counting their scores.我正在制作一个游戏,玩家必须输入数字来猜测一个国家的人口,但我在计算他们的分数时遇到了问题。 I have this if statement:我有这个 if 语句:

 if (player1 === "" || player2 === "") {
          resultSpan.innerHTML = "One or two empty values";
        } else {
          resultSpan.innerHTML = `The correct number is ${population.toLocaleString()}`;
          let playerWin = document.querySelector(".results-player-win");

          if (
            Math.abs(parseInt(population) - playerOneTotal) <
            Math.abs(parseInt(population) - playerTwoTotal)
          ) {
            playerWin.innerText = "Player 1 wins!";
            playerOneScore++;
          }

          if (
            Math.abs(parseInt(population) - playerOneTotal) >
            Math.abs(parseInt(population) - playerTwoTotal)
          ) {
            playerWin.innerText = "Player 2 wins!";
            playerTwoScore++;
          }
          document.getElementById(
            "current-score"
          ).innerHTML = `P1: ${playerOneScore} --- P2: ${playerTwoScore}`;
        }

But, instead of incrementing the score by 1, sometimes it's two or three.但是,有时不是将分数增加 1,而是 2 或 3。 I'm guessing this is because another eventListener is being called beforehand.我猜这是因为预先调用了另一个eventListener However, I can't find a way to separate them completely, as they both need to use the same population variable for the score to be calculated correctly.但是,我找不到将它们完全分开的方法,因为它们都需要使用相同的总体变量才能正确计算分数。

I've tried adding this.remove() inside of the IF statements, but it deletes the button.我试过在 IF 语句中添加this.remove() ,但它删除了按钮。 I've also tried e.stopImmediatePropagation();我也试过e.stopImmediatePropagation(); and creating a variable isFirst , and setting to true then false, but they block the code completely on second execution.并创建一个变量isFirst ,然后设置为 true 然后 false,但是它们在第二次执行时完全阻止了代码。

For more clarity, I'm also posting a JSFiddle .为了更清楚,我还发布了一个JSFiddle

I managed to fix the issue by initializing the variables我设法通过初始化变量来解决这个问题

playerOneScore = 0;
playerTwoScore = 0

; ; (the ones that track the score) in the global environment. (跟踪分数的那些)在全球环境中。 I also added { once: true } to the listener that tracks user input/scores as clicking the button multiple times would cause unwanted behaviour.我还将{ once: true }添加到跟踪用户输入/分数的侦听器中,因为多次单击按钮会导致不必要的行为。

暂无
暂无

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

相关问题 一旦在 JavaScript 中满足条件,我将如何使事件处理程序函数中的被调用函数无法运行? - How would I void a called function within an event Handler function from running once a condition is met in JavaScript? 如何退出单击 function 或防止 function 在满足条件之前触发 - How can I exit a click function or prevent the function from triggering until a condition is met 如何在遇到某个条件时阻止onbeforeunload触发? - how can i prevent onbeforeunload from firing when a certain condition is met? jQuery:防止绑定的js单击运行,如果满足条件,则运行它 - jQuery: Prevent bound js from running on click, then running it if condition is met 如果满足条件,我如何运行第二次if语句一次 - How do i run the second if statement once if the condition is met and then never again 如果满足条件,我该如何“重启” if语句? - How do I “restart” if statement if a condition is met? 一旦满足一个案例条件,如何在switch语句中更改案例? - How to change cases in a switch statement once one case condition is met? 当同时触发多个更改事件时,如何防止函数多次运行? - How can I prevent a function from running more than once when multiple change events are fired at once? 满足条件时阻止Jquery图像工具运行 - Prevent Jquery image tool from running when condition met 满足特定条件后如何禁用单击事件? - How do I disable the click event once a certain condition is met?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM