JavaScript is single-threaded; setTimeout callback won't be called when you're blocking the main event loop with that while (while (t) { ... }).
If you're running your code in browser, you can't really do anything about it but writing your code in other way;
Instead of blocking the main event loop, you can use Promises Pattern.
If you're running your code in something like node you can use native modules, making you able to create threads (like threads_a_gogo.)
javascript - How to break while loop outside it - Stack Overflow
javascript - Breaking out of a while loop and switch in one go - Stack Overflow
Stopping a while loop after a certain time: much harder than expected
javascript - how do I break while loop if a condition is not met? - Stack Overflow
Videos
JavaScript is single-threaded; setTimeout callback won't be called when you're blocking the main event loop with that while (while (t) { ... }).
If you're running your code in browser, you can't really do anything about it but writing your code in other way;
Instead of blocking the main event loop, you can use Promises Pattern.
If you're running your code in something like node you can use native modules, making you able to create threads (like threads_a_gogo.)
Because the while loop never exits, your other code is never run when something is done synchronously. My best offer for you would be not to use a while loop and instead have a recurring event such as setTimeout and make the timeout run itself when complete. This way you're not creating a closed environment.
You can use labels with the break statement in js
var input = prompt();
outside:
while(true) {
switch(input) {
case 'hi':
break;
case 'bye':
//I want to break out of the switch and the loop here
break outside;
}
/*Other code*/
}
Wrap the whole thing in a function, then you can simply return to break out of both.
var input = prompt();
(function () {
while(true) {
switch(input) {
case 'hi':
break;
case 'bye':
return;
}
/*Other code*/
}
})();
Here's my code:
var timerIsRunning = true;
while (timerIsRunning) {
console.log('Timer is running.');
}
//stops timer after 10s
setTimeout(function(){
timerIsRunning=false;
}, 10000);You'd think this would work. But the while loop keeps running forever and I have no idea why.
There is a check (kind of hacky) that I've seen being used. It uses Date to keep track of the time a loop is using. It doesn't check if a loop is really infinite or not, it just checks wether it taking too long or not:
function isInfiniteProcess(a, b) {
var t0 = Date.now();
while (a !== b) {
a++;
b--;
if(Date.now() - t0 > 5000) { // if this loop keeps running for 5 seconds and not finished yet
return true; // break it and return true
}
}
return false; // otherwise return false (the loop finished before 5 seconds has passed)
}
if(isInfiniteProcess(2, 3))
console.log("The loop may be infinite...");
This isn't really reliable, but it helps detect if a loop could be infinite. The threshold (here 5 seconds) is totally up to you to choose.
Note: As @snap mentioned in his comment bellow, you can use an iterations counter instead of time delta:
var counter = 0;
while(...) {
...
if(++counter > 1000000) { // after the 1000000th iteration
return true; // assume the loop is infinite and break it
}
}
As for the other approach, the threshhold (here 1000000 iterations) is really up to you to choose.
your loop will always go to infinite if a becomes greater than b. so to Exit the while loop you can use break like this:
function isInfiniteProcess(a, b) {
while (a !== b) {
a++;
b--;
if(a > b) break;
}
if(a > b) return true;
}