简体   繁体   English

在Javascript循环期间强制重绘

[英]Force repaint during Javascript loop

I want to force a repaint during a Javascript loop. 我想在Javascript循环中强制重绘。 For example: 例如:

var s = document.getElementById("foo");
for (var i = 0; i < 10000; i++)
    s.innerText = i + " times";

That updates at the end, but I would like to observe each intermediate step. 最后更新,但是我想观察每个中间步骤。 The common response to this problem is to use setTimeout , however, there are three reasons that is unacceptable: 解决此问题的常见方法是使用setTimeout ,但是有三个不可接受的原因:

  • There are other buttons/links etc on the page, and it is important that none of these can be clicked while the loop operates. 页面上还有其他按钮/链接等,重要的是在循环运行时,不能单击其中的任何一个。
  • At the end of the loop I want to display a popup, if the timeout fires then standard popup blockers will hide it, as it is no longer initiated by the user. 在循环的最后,我想显示一个弹出窗口,如果超时触发,则标准的弹出窗口阻止程序将其隐藏,因为它不再由用户启动。
  • If the user clicks while the loop is running, I want that click to be buffered until the loop completes, not fired half way through the loop. 如果用户在循环运行时单击,我希望该单击缓冲在循环结束之前,而不是在循环进行一半时触发。

Is there a way to force the browser to repaint? 有没有办法强制浏览器重新粉刷?

I'm afraid this is your only solution though, since the non-repaintable-in-the-middle is something important as a predicate for speed gains in rendering of browsers. 恐怕这是您唯一的解决方案,因为不可重画的中间内容作为提高浏览器渲染速度的谓词很重要。

Perhaps could you manage a buffer-me flag in those click events you don't want to fire? 也许您可以在您不想触发的那些单击事件中管理“缓冲我”标志? Or better said a dont-buffer-me flag... 或者更好的说一个不要缓冲我的标志...

I hope it helped, best regards. 希望对您有所帮助。

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

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