I'm trying to learn while-loops in Javascript. Why won't this while loop run even when the button is clicked and the boolean becomes true? Is it even possible to have while-loops react to event-driven variable changes?
let run = false;
let count = 0;
const button = document.getElementById("startstop");
const counter = document.getElementById("counter");
button.addEventListener('click', function() {
run = !run; //clever way to toggle the boolean
})
while (run == true) {
count++;
counter.innerHTML = count;
}
<button id="startstop">Start/Stop</button>
<p id="counter">0</p>