在JavaScript中强制DOM刷新 [英] Force DOM refresh in JavaScript

查看:629
本文介绍了在JavaScript中强制DOM刷新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




  • 运行重型计算(需要1-3s)

    ($(#results))$ / code code code code code $ >



问题是,在循环结束之前,DOM不会刷新。运行循环100次迭代这需要太长时间,我想在每次计算一次时给出结果。



如何强制DOM被刷新? / p>

解决方案

如果您在支持它的浏览器中, worker 是这种情况的理想选择。



如果没有,请使用 setTimeout 每次计算后打破callstack并允许浏览器使用渲染时间。


I am running a loop within a click event handler doing the following:

  • run heavy computation (takes 1-3s)
  • call $("<li> + data + </li>").appendTo($("#results"))

The problem is, that the DOM is not refreshed until the loop has terminated. Running the loop for 100 iterations this takes too long, I would like to give a result every time I computed one.

How can I force the DOM to be refreshed?

解决方案

If you are in a browser that supports it, workers are ideal for this situation.

If not, use setTimeout after each computation to break the callstack and allow the browser to use time on rendering.

这篇关于在JavaScript中强制DOM刷新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆