JS Bin while infinite loop

2019-07-27 03:58发布

问题:

I noticed strange behaviour of JS Bin runner when I execute the following code:

var ask = prompt("test");

while(ask!=="yes"){
    ask = prompt("test");
}

I get this kind of error:

Exiting potential infinite loop at line 4. To disable loop protection: add "// noprotect" to your code

I was wandering why is that happening ? (The execution of that code works fine in site code)

回答1:

There is an unsolvable problem in computer science called The Halting Problem.

In short, it means that a computer cannot look at a piece of code and figure out if it will go into an endless loop or end at some time. However, they can make some guesses about it and warn you if they spot anything that might be dangerous.

Not is. Might be.

This is one of those cases. The system is warning you and you have to say "Yea, yea, I heard you. Do it anyways."[*]

[*] This, of course, is to prepare us for a Star-Trek Universe where we disable the safety on everything to save the day.



回答2:

prompt, alert, confirm.

These JS functions are synchronous, that means browser will stop doing anything and will hold/wait for the user response. Since you also possibly created a infinite loop by prompting again by checking the result of previous prompt, it throws that message.

This infinite loop will make the browser nonresponsive