Solution (thanks to @MrMT's answer in comments):
var isPaused = false;
// replaced a random number with a counter to check that it doesn't reset
let num = 0;
function draw() {
function loop() {
if (isPaused === false) {
num++;
testArea.textContent = `${num}`;
}
else {
return;
}
setTimeout(() => { requestAnimationFrame(loop); }, 200);
return;
}
loop();
}
function pauseMenu() {
pauseScreen.classList.remove("hide");
console.log("paused");
isPaused = true;
resumeButton.addEventListener("click", pauseResume);
}
function pauseResume() {
isPaused = false;
console.log("unpaused");
pauseScreen.classList.add("hide");
// otherwise after passing over if (isPaused === false)
// current line will exit the loop altogether!
draw();
}
It worked in my canvas animation code too with a few edits. Much appreciated!