JS Bin while infinite loop

混江龙づ霸主 提交于 2019-12-11 10:35:29

问题


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



来源:https://stackoverflow.com/questions/23448485/js-bin-while-infinite-loop

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!