我有一个警告弹出,询问“你想玩吗?”如果答案是真的,它会运行一些代码。但是点击“是”后我希望按下一个按钮,我的代码继续运行而不等待按下它。
以下是我的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>
答案 0 :(得分:1)
您只是在侦听要选择的确认按钮,而不是要点击的按钮。
在下面的代码中,我已将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>
答案 1 :(得分:0)
尝试
if (start == true) {
而不是
while (start) {