[英]need to click twice on a button
我編寫了一個簡單的游戲,在其中我使用了啟動按鈕來啟動新游戲或進行其他游戲。
第一次單擊(開始新游戲)效果很好。 然后,當我想做另一個游戲(通過停止並重新初始化當前游戲)時,它也可以工作。
我的問題在於后一個游戲的重新初始化(因此,第二次重新啟動):我必須兩次單擊引導按鈕。
這是Javascript代碼段(引導按鈕為buttonNewGame
):
var buttonNewGame = document.getElementById('buttonNewGame');
function startGame() {
// If currentGame then call initGame
if (isCurrentGame) {
isCurrentGame = false;
initGame();
console.log('HERE : stop and reinitialize current game');
}
else {
isCurrentGame = true;
}
// Call main function
currentGame();
}
對於需要單擊兩次的情況, console.log('HERE : stop and reinitialize current game');
第一次單擊不顯示任何內容,然后在第二次單擊時調用startGame()
。
這與焦點問題或自舉按鈕有關嗎?
謝謝
如果遵循執行順序,您會注意到,第一次進入startGame()
, isCurrentGame
的值是false
,這就是為什么它不打印console.log
。
第一次進入startGame()
, isCurrentGame
的值更改為true,這就是為什么第二次進入時的原因。 它進入if條件內。
為了解決這個問題,您需要在調用按鈕時將isCurrentGame
的值重置為true。
第一次:
isCurrentGame
值為false
,因此它將進入else並被分配為true。
if (isCurrentGame) {
isCurrentGame = false;
initGame();
console.log('HERE : stop and reinitialize current game');
} else
isCurrentGame = true;
第二次進入時, isCurrentGame
為true
,因此它將進入if條件並調用initGame()
並輸出console.log
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.