gpt4 book ai didi

javascript - 如何使用 clearInterval() 和 location.reload()?

转载 作者:行者123 更新时间:2023-11-28 00:04:57 25 4
gpt4 key购买 nike

我在让我的应用程序正常运行时遇到了一些问题。这是一个问答应用程序,带有一个简单的倒计时计时器。我的问题是当我想在 setInterval() 运行时重置游戏(重新加载页面)时,它起作用了。但是,如果 setInterval()clearInterval() 停止并且我的 Game Over 元素显示,如果我点击重置,期望调用 location.reload( ),页面自动再次启动计时器。这是某种我似乎找不到的循环问题,但也许您的专业人士可以发现我的代码中的错误。

我已尝试通过多种方式重构我的代码以隔离错误,但我似乎无法找到它。

// DOM ELEMENTS ========================================================

const startReset = document.getElementById("startReset");
const scoreValue = document.getElementById("score");
const timer = document.getElementById("timer");
const timerValue = document.getElementById("timerValue");
const gameOver = document.getElementById("gameOver");
const correct = document.getElementById("correct");
const wrong = document.getElementById("correct");


// GLOBAL GAME VARIABLES ===============================================

let playing = false;
let score;
let action;
let timeRemaining;

// GAME OPERATIONS =====================================================

// Start/Reset Game
startReset.onclick = function() {
// Reset Game
if(playing === true) {

// Reload page
location.reload();

}
// Start Playing
else {
// Change mode from not playing to playing
playing = true;

// Take Away Game Over Element
gameOver.style.visibility = "hidden";

// Change start button to reset button
startReset.innerHTML = "Reset Game";

// Put score back to 0
score = 0;

// Update HTML with score
scoreValue.innerHTML = score;

// Display timer
timer.style.display = "block";
timeRemaining = 60;

// Start Countdown
startCountdown();
}
}

// Start Countdown Function
const startCountdown = () => {
// Set timer
action = setInterval(function(){
// reduce time by 1 sec
timeRemaining -= 1;
// update html
timerValue.innerHTML = timeRemaining;
// check if time is out
if(timeRemaining === 0) {
stopCountdown();
gameOver.style.visibility = "visible";
timer.style.display = "none";
correct.style.display = "none";
wrong.style.display = "none";
playing = false;
}
}, 100);
};

// Stop Countdown Function
const stopCountdown = () => {
clearInterval(action);
};
=============================================================
<body>
<div id="logoContainer">
<img src="./images/gotLogo.png">
</div>

<div id="container">

<div id="scoreCounter">
Score: <span id="score">0</span>
</div>

<div id="correct">Correct</div>

<div id="wrong">Try Again</div>

<div id="question">
<p>Click Start to Begin!</p>
</div>

<div id="instruction">
Click on the correct answer!
</div>

<div id="choices">

<div id="choice1" class="choice"></div>
<div id="choice2" class="choice"></div>
<div id="choice3" class="choice"></div>
<div id="choice4" class="choice"></div>

</div>

<div id="startReset">
Start Game
</div>

<div id="timer">
Time Remaining: <span id="timerValue">60</span> sec
</div>

<div id="gameOver">
<p>GAME OVER!</p>
<p>YOUR SCORE IS: <span id="finalScore">0</span></p>
</div>
</div>

<script src="app.js" type="text/javascript"></script>
</body>

</html>

我希望在 Game Over 出现后单击 Reset Game 会重新加载页面并强制重新单击以开始游戏,但是,它会重新加载页面并自动重新启动计时器。如果我在计时器运行时单击 Reset Game,它会按预期工作。

最佳答案

已找到答案。也许这会对其他人有所帮助。

在我的 startCountdown() 函数中,在 if 语句中,我最初重置了 playing = false,这短路了我上面在 Start/Reset game 下声明的 onclick 函数的条件逻辑。去掉它可以让游戏正常运行!

关于javascript - 如何使用 clearInterval() 和 location.reload()?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55764287/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com