使用 javascript 循环渲染文本 - 浏览器无响应
我有一段 javascript,它会循环多次计算内容,它应该在每次迭代时输出一些文本来屏幕,但它会消失很长一段时间,并立即将所有内容刷新到屏幕上。
可能有一些已知的技巧可以让东西迭代更新,而不是挂在输出上然后刷新,问题是我不知道它们!
任何帮助表示赞赏。
更新:我按照你们中的一些人的建议更改了我的代码以使用 setTimeout ,但恐怕这没有帮助。在处理完成之前我仍然看不到任何东西。我也尝试增加超时,但效果仍然相同(通过增加每次迭代之间的超时,显然需要更多时间,但浏览器仍然没有响应)。有什么想法吗?
I have a piece of javascript that loops a number of times calculating stuff and it is supposed to output some text to screen every iteration, but instead it goes away for a long time and flushes everything at once to screen.
There probably are known tricks to get the thing to iteratively update rather than hanging onto the output then flushing, problem is I don't know them!
Any help appreciated.
UPDATE: I altered my code to use setTimeout as some of you suggested but I am afraid it's not helping. I still I won't see anything till the processing is complete. I also tried increasing the timeout but the effect is still the same (by increasing the timeout between each iteration it obviously takes more time but still the browser goes unresponsive). Any idea?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这听起来像是预期的行为。 JavaScript 的运行速度可能比浏览器渲染其输出的速度快。
听起来您想显示操作的进度列表?也许您可以将它们全部显示在页面上,使用
display: none
并将它们切换到display: block
可能会更快。或者,如果您不想要绝对精度,请使用
setTimeout()
。That sounds like expected behaviour. The JavaScript is probably running faster than the browser can render its output.
It sounds like you want to show a progress list of actions? Perhaps you could have them all on the page, with
display: none
and switch them todisplay: block
may be faster.Alternatively, if you don't want absolute precision, play with
setTimeout()
.您应该假设当您的代码运行时,屏幕上的视图不会更新(在最坏的情况下,甚至浏览器用户界面也将没有响应)。您需要将处理分成多个步骤,并让浏览器偶尔处理事件。我不知道你的确切问题是什么,但这里有一个应该接近的例子。
超时可能非常小。使用超时 0 可以让浏览器处理事件,然后立即返回到您的代码。只需记住让浏览器足够频繁地处理事件,以便页面保持响应。
最近有一些与支持浏览器中的线程相关的工作,但这种东西还没有得到所有大型浏览器的真正支持。
You should assume that while your code is running, the view on the screen will not be updated (and in the worst case, even the browser user interface will be unresponsive). You'll need to split your processing into steps, and let the browser process events once in a while. I don't know what your exact problem is, but here's an example of something that should be close.
The timeout can be really small. Using a timeout of 0 lets the browser process events and then immediately return to your code. Just remember to let the browser process events often enough so the page remains responsive.
There has been some work lately related to supporting threads in the browser, but that kind of stuff isn't really supported by all the big browsers yet.
setTimeout()、setInterval() 是在 JavaScript 中实现等待/暂停功能的最佳方法。
下面是显示 10 个数字的示例程序,每个数字之间的时间间隔为 100 毫秒。
将此代码粘贴到空白 html 文件中进行检查。
setTimeout(), setInterval() are the best ways to implement wait/pause function in javascript.
Here is sample program to dispay 10 number with 100 millisecs time interval between each.
Paste this code into a blank html file to check it.
如果您只需要支持前沿浏览器,请使用 Web Workers。
If you only need to support bleeding-edge browsers, use Web Workers.