首页 > 解决方案 > 评分停止并在 javascript 上重置游戏

问题描述

所以我基本上是一名手动测试人员,现在正试图提高 javascript 的技能。我正在开发一个名为 Hand cricket 的 javascript 迷你项目。这只是我和朋友们玩的童年游戏。所以我在这里有一个函数可以检查玩家是否出局。如果玩家出局,我无法停止游戏,但用户仍然可以提供输入。请帮助我,一旦玩家退出,我应该如何停止程序。它应该会在几秒钟后停止游戏并重置玩家分数。下面是代码片段......

我已宣布玩家得分为

  ////// player score initialised to zero

  let pScore = 0;

  // checking if the player is out

  //comparing hands

    const compareHands = (playerChoice, computerChoice) => {
  //updating text
    const winner = document.querySelector(".winner");
    
  //checking player is out

    if (playerChoice === computerChoice) {
        winner.textContent = "You are out!";    
        return;
    }
    else {
        updateScore(playerChoice);
    }
};

更新计分板:

  const updateScore = (playerChoice) => {
    let playerScore = document.getElementById("playerScore").innerHTML;
    let scores = getScoreInDigits(playerChoice)
    playerScore = parseInt(playerScore) + scores.pScore;
    document.getElementById("playerScore").innerHTML = playerScore;
}

标签: javascript

解决方案


尝试在return之前禁用按钮:

button.setAttribute("disabled", true);

推荐阅读