简体   繁体   English

javascript 如何与 while(true)-loop 一起工作?

[英]How does javascript work with while(true)-loop?

Why does an infinite loop (even if you insert an alert and manually close it) cause the page to freeze?为什么无限循环(即使您插入警报并手动关闭它)会导致页面冻结? Its work fine for microcontrollers, but not for browsers.它适用于微控制器,但不适用于浏览器。 Why?为什么?

while(true) {
    alert();
}

In my understanding, there should be an endless call of a new alert when the old one is closed.在我的理解中,当旧警报关闭时,应该会不断地发出新警报。 This happens for a while, but then the browser freezes.这会发生一段时间,但随后浏览器会冻结。

Yes, I know about the existence of requestAnimationFrame and setInterval, but I want to understand what is the problem with infinite loops.是的,我知道requestAnimationFrame和setInterval的存在,但是我想了解无限循环的问题是什么。

Ok, guys, I found answer with yours help: Here its descripbed well:好的,伙计们,我在您的帮助下找到了答案:这里描述得很好:

Concurrency model and the event loop : https://developer.mozilla.org/en-US/docs/Web/JavaScript/EventLoop并发 model 和事件循环https://developer.mozilla.org/en-US/docs/Web/JavaScript/EventLoop

Quote from "Run-to-completion" part:引用“运行到完成”部分:

"...A downside of this model is that if a message takes too long to complete, the web application is unable to process user interactions like click or scroll. The browser mitigates this with the "a script is taking too long to run" dialog. A good practice to follow is to make message processing short and if possible cut down one message into several messages." “...此 model 的一个缺点是,如果一条消息需要很长时间才能完成,则 web 应用程序无法处理用户交互,如单击或滚动。浏览器通过“脚本运行时间过长”来缓解这种情况对话。一个好的做法是缩短消息处理时间,如果可能的话,将一条消息缩减为多条消息。”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM