The problem is with how javascript is executed. Basically everything that is regarding your website is executed in one Thread (note: this isn't completely right, but it works for us), so when you have a blocking javascript script (like a while loop) it freezes your whole website.
If you now press the stop button, it won't execute, because your while-loop is still freezing the page.
The Solution:
Use setInterval instead like this:
var interval_handle = undefined; //<- id of your interval. Used to stop it again
function start() {
stopMayInterval(); // Stop the loop, if already running
interval_handle = setInterval( // Start the loop
()=>{ // Function that is looped
console.log("start");
}
,1); looped every 1 ms;
}
function stop() {
stopMyInterval();
console.log("stop")
}
/* Stops the Interval */
function stopMyInterval(){
if(interval_handle != undefined){
clearInterval(interval_handle);
interval_handle = undefined;
}
}
If you use it like this, javascript automatically queues your function every 1 ms after it has done every other thing it needs to execute for your site to work (like a button event).
Documentation: https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/setInterval