在继续之前等待按钮单击
Wait for a button click before proceeding
我有一个警告弹出询问 "would you like to play" 如果答案是真的它 运行 一些代码。但是在您单击 "yes" 之后,我希望按下一个按钮,并且我的代码会继续 运行 而无需等待它被按下。
这是我的 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>
尝试
if (start == true) {
而不是
while (start) {
您只监听要选择的确认按钮,而不是要点击的按钮。
在下面的代码中,我将 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>
我有一个警告弹出询问 "would you like to play" 如果答案是真的它 运行 一些代码。但是在您单击 "yes" 之后,我希望按下一个按钮,并且我的代码会继续 运行 而无需等待它被按下。
这是我的 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>
尝试
if (start == true) {
而不是
while (start) {
您只监听要选择的确认按钮,而不是要点击的按钮。
在下面的代码中,我将 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>