当我的脚本做一些重的处理时,如何强制浏览器重绘? [英] How can I force the browser to redraw while my script is doing some heavy processing?

查看:129
本文介绍了当我的脚本做一些重的处理时,如何强制浏览器重绘?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 innerHTML 属性动态修改 DIV ,以报告需要几个秒完成。问题在于,在Firefox之后,该页面实际上不会重新呈现以反映该更改,直到脚本完成为止。这使得应用程序感到迟钝。有没有办法确保HTML的更改立即显示,即使更多脚本正在运行?

I am using the innerHTML property to modify a DIV dynamically, to report on a process that takes a few seconds to finish. The problem is that on Firefox the page is not actually re-rendered to reflect that change until after the script has finished. This makes the app feel sluggish. Is there a way to make sure that changes to the HTML show up immediately, even if more scripts are running?

推荐答案

浏览器是单线程的。脚本正在运行时,浏览器无法执行任何其他操作。如果要进行类似进度表的操作,则必须使用 setTimeout() setInterval () ,并将您的任务分解成更小的块,在一段时间内运行。这在脚本运行之间留下了空白,控制回浏览器,浏览器可以重绘。

The browser is single threaded. While the script is running, the browser can't do anything else. If you want to do something like a progress meter, you have to use setTimeout(), or setInterval(), and break your task down into smaller chunks that get run on an interval. This leaves gaps in between the script runs, giving control back to the browser, where the browser can redraw.

这篇关于当我的脚本做一些重的处理时,如何强制浏览器重绘?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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