2015-06-11 137 views
1

我有一個彈出的提示,詢問「你想玩嗎」,如果答案是真的,它會運行一些代碼。但是當你點擊「是」後,我希望按下一個按鈕,並且我的代碼繼續運行而不用等待它被按下。等待點擊按鈕才能繼續

這裏是我有,如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>

+1

做的是使你的遊戲的的jsfiddle(https://jsfiddle.net/)和共享的鏈接,使人們對這裏可以一看的最好的事情在它並正確地爲您調試它。 – Giuseppe

+0

好吧現在就試試:) –

+0

https://jsfiddle.net/3cuwnafz/ –

回答

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>

0

嘗試

if (start == true) { 

,而不是

while (start) {