79239290

Date: 2024-11-30 10:16:09
Score: 2
Natty:
Report link

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!

Reasons:
  • Blacklisted phrase (0.5): thanks
  • Blacklisted phrase (1): appreciated
  • Whitelisted phrase (-1): It worked
  • Long answer (-0.5):
  • Has code block (-0.5):
  • User mentioned (1): @MrMT's
  • Self-answer (0.5):
  • Low reputation (1):
Posted by: touchofstatic