gpt4 book ai didi

javascript - 等待按钮点击后再继续

转载 作者:行者123 更新时间:2023-11-28 00:12:21 25 4
gpt4 key购买 nike

我弹出一个警报,询问“你想玩吗”,如果答案为真,它会运行一些代码。但是在您单击"is"之后,我希望按下一个按钮,并且我的代码继续运行而不等待按下它。

这是我的 Javascript:

var start = confirm("Would you like to Play?");
var plays = 0;
var playerScore = 0;
var computerScore = 0;
var userChoice = ""
function choose(buttonChoice) {
userChoice = buttonChoice;
}

while(start) {

var pScoreDiv = document.getElementById('pScore');
pScoreDiv.innerHTML = playerScore;

var cScoreDiv = document.getElementById('cScore');
cScoreDiv.innerHTML = computerScore;

console.log(playerScore);
console.log(computerScore);

var computerChoice = Math.random();

if (computerChoice < 0.34) {
computerChoice = "rock";
} else if(computerChoice <= 0.67) {
computerChoice = "paper";
} else {
computerChoice = "scissors";
}
}
<button onClick="choose('rock')">Rock</button>
<button onClick="choose('paper')">Paper</button>
<button onClick="choose('scissors')">Scissors</button>

最佳答案

您只监听要选择的确认按钮,而不是要单击的按钮。

在下面的代码中,我将 while 更改为一个函数,用于检查 start 是否已设置,并在单击按钮时调用。

var start = confirm("Would you like to Play?");
var plays = 0;
var playerScore = 0;
var computerScore = 0;
var userChoice = ""

function choose(buttonChoice) {
userChoice = buttonChoice;
play();
}

function play() {
if (start) {
var pScoreDiv = document.getElementById('pScore');
pScoreDiv.innerHTML = playerScore;

var cScoreDiv = document.getElementById('cScore');
cScoreDiv.innerHTML = computerScore;

console.log(playerScore);
console.log(computerScore);

var computerChoice = Math.random();

if (computerChoice < 0.34) {
computerChoice = "rock";
} else if (computerChoice <= 0.67) {
computerChoice = "paper";
} else {
computerChoice = "scissors";
}
}
}
<button onClick="choose('rock')">Rock</button>
<button onClick="choose('paper')">Paper</button>
<button onClick="choose('scissors')">Scissors</button>

关于javascript - 等待按钮点击后再继续,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30775732/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com