JS Bin while infinite loop

2019-07-27 04:17发布

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)

2条回答
兄弟一词,经得起流年.
2楼-- · 2019-07-27 04:38

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

查看更多
Deceive 欺骗
3楼-- · 2019-07-27 04:55

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.

查看更多
登录 后发表回答