2017-09-16 98 views
-2

我正在嘗試使用戶無法按下數字按鈕,直到按下開始按鈕。我搜索了w3schools和其他網站,但找不到解決方案。任何幫助將不勝感激,即使你可以指向我的網站。我的講師已經通知我們,我們需要在網上找到解決我們問題的任何解決方案。即使對於一本好JavaScript書籍的建議也會有所幫助,因爲沒有教科書,他也沒有教它。setTimeOut爲javascript中的按鈕cliks

<body> 
<h3 style="margin-left: 15px">This is Your TARGET:</h3> 
<div id="randomNum" class="display" style="margin-top: -20px; margin-bottom: 30px">0</div> 

<!-- Start button --> 
<button onclick="setTimeout(myFunction, 5000);">Start</button> 

<!-- Total value text --> 
<div id="text_total">0</div> 

<!-- Number buttons --> 
<div class="number_button" onclick="change_total(1)">1</div> 
<div class="number_button" onclick="change_total(2)">2</div> 
<div class="number_button" onclick="change_total(3)">3</div> 
<div class="number_button" onclick="change_total(4)" style="clear: left">4</div> 
<div class="number_button" onclick="change_total(5)">5</div> 
<div class="number_button" onclick="change_total(6)">6</div> 
<div class="number_button" onclick="change_total(7)" style="clear: left; margin-bottom: 30px">7</div> 
<div class="number_button" onclick="change_total(8)" style="margin-bottom: 30px">8</div> 
<div class="number_button" onclick="change_total(9)" style="margin-bottom: 30px">9</div> 

<h3 style="clear: left; margin-left: 58px">COUNTER!</h3> 
<div id="counter" class="display" style="clear: left; margin-top: -20px">0</div> 

<script> 
// Variables 
var total = 0; 
var target; 
var clicks = 0; 



window.onload = randomNumber(); 

// Functions 
function change_total(arg) { // This takes button input and changes the total value 
    total = total + arg; 
    clicks = clicks + 1; 
    update_total(); 
    if (total == target) { 
     alert("You win!"); // popup window with message 
     total = 0; // reset for next round 
     clicks = 0; // resets the click counter 
     randomNumber(); //gets new number for next round 
     update_total(); 
    }if (total > target) { 
     alert("BUSTED!!"); 
     total = 0; 
     clicks = 0; 
     randomNumber(); 
     update_total(); 
     } 
    update_clicks(); 
} 

function myFunction() { 
    alert("You failed to reach the target in time!"); 
} 

function update_total() { // Updates the text on the screen to show the current total 
    document.getElementById("text_total").innerHTML = total; 
} 

function randomNumber() { // returns a random number between 25 and 75 
    target = Math.floor(Math.random() * (50) + 25); 
    document.getElementById("randomNum").innerHTML = target; 
} 

function update_clicks() { // lets user know how many clicks 
    document.getElementById("counter").innerHTML = "You clicked the mouse " + clicks + " times."; 
} 

</script> 

</body> 
+0

這是不相關的問題,但請注意,你的'randomNumber()'函數中的25〜〜74的(不是75)包括返回一個整數。這是因爲'Math.random()'返回的值小於或等於0,_less than_ 1。在這裏使用'Math.floor()'是正確的,如果需要的話可以考慮這一點。 –

+0

而不是使用超時間隔,如果在開始按鈕之前按下某個鍵,您只是顯示一條錯誤消息? –

回答

0

你可以試試這個,

我已經測試過它在我的結束,下面看到的,

我只是使用ISSTART布爾變量來檢查是遊戲開始與否。

<body> 
<h3 style="margin-left: 15px">This is Your TARGET:</h3> 
<div id="randomNum" class="display" style="margin-top: -20px; margin-bottom: 30px">0</div> 

<!-- Start button --> 
<button onclick="startGame()">Start</button> 

<!-- Total value text --> 
<div id="text_total">0</div> 

<!-- Number buttons --> 
<div class="number_button" onclick="change_total(1)">1</div> 
<div class="number_button" onclick="change_total(2)">2</div> 
<div class="number_button" onclick="change_total(3)">3</div> 
<div class="number_button" onclick="change_total(4)" style="clear: left">4</div> 
<div class="number_button" onclick="change_total(5)">5</div> 
<div class="number_button" onclick="change_total(6)">6</div> 
<div class="number_button" onclick="change_total(7)" style="clear: left; margin-bottom: 30px">7</div> 
<div class="number_button" onclick="change_total(8)" style="margin-bottom: 30px">8</div> 
<div class="number_button" onclick="change_total(9)" style="margin-bottom: 30px">9</div> 

<h3 style="clear: left; margin-left: 58px">COUNTER!</h3> 
<div id="counter" class="display" style="clear: left; margin-top: -20px">0</div> 

<script> 
// Variables 
var total = 0; 
var target; 
var clicks = 0; 



window.onload = randomNumber(); 
var isStart=false; 
// Functions 
function change_total(arg) { // This takes button input and changes the total value 
if(isStart){ 
    total = total + arg; 
    clicks = clicks + 1; 
    update_total(); 
    if (total == target) { 
     alert("You win!"); // popup window with message 
     total = 0; // reset for next round 
     clicks = 0; // resets the click counter 
     randomNumber(); //gets new number for next round 
     update_total(); 
     isStart=false; 
    }if (total > target) { 
     alert("BUSTED!!"); 
     total = 0; 
     clicks = 0; 
     randomNumber(); 
     update_total(); 
     isStart=false; 
     } 
    update_clicks(); 
    } 
    else{ 
    alert('please start the game'); 
    } 
} 

function myFunction() { 
    alert("You failed to reach the target in time!"); 
} 

function update_total() { // Updates the text on the screen to show the current total 
    document.getElementById("text_total").innerHTML = total; 
} 

function randomNumber() { // returns a random number between 25 and 75 
    target = Math.floor(Math.random() * (50) + 25); 
    document.getElementById("randomNum").innerHTML = target; 
} 

function update_clicks() { // lets user know how many clicks 
    document.getElementById("counter").innerHTML = "You clicked the mouse " + clicks + " times."; 
} 
function startGame(){ 
isStart=true; 
} 
</script> 

</body> 
+0

謝謝你,似乎在我的工作。現在我需要在定時器部分工作。 – DonB

+0

但之前,喜歡和投票我的答案。 –