I've been making ping pong game watching tutorial and in the end decided to enhance a bit by adding paused button, I added and everything worked just fine but when I restart the game it was still being paused. Then I decided that I needed to add a paused check variable paused = false; and for some reason the state is the same, please help me!
const resetBtn = document.querySelector('#resetBtn');
const pauseBtn = document.querySelector('#pauseBtn');
paused = false;
running = true;
pauseBtn.addEventListener('click', pauseGame);
resetBtn.addEventListener('click', resetGame);
gameStart();
function gameStart(){
createBall();
nextTick();
};
function nextTick(){
if(running && !paused){
intervalID = setTimeout(() => {
// some code
nextTick();
}, 10)
}
else{
displayPause();
}
};
function resetGame(){
// some code
paused = false;
// some code
updateScore();
clearInterval(intervalID);
gameStart();
};
function pauseGame(){
running = !running;
paused = true;
if(running){
nextTick();
}
};
<button class="buttons" id="pauseBtn">pause</button>
<button class="buttons" id="resetBtn">reset</button>
I'm expecting to resume game when user press restart button.


You don't need both a
runningandpausedvariable. They are essentially the same thing. Just userunningsince you've already got that working.Since you are using
setTimeoutto run your game, you need to useclearTimeoutwhen to clear it, notclearInterval. If you were usingsetTintervalthat's when you'd want to useclearIntervalto clear it.In the below code I've changed your timeout amount to
100just so it's easier to see what's happening in the console, it goes by too quickly at10. You should probably keep it at10for your game though