[英]JavaScript Script Taking Too Long to Execute. Getting the Script Taking Too Long Prompt
I have a JavaScript function buildTable which builds a very long HTML table. 我有一个JavaScript函数buildTable,它构建了一个非常长的HTML表。
function buildTable() {
for (var i = 0; i < rowCount; i++) {
.. code to create table
}
} }
The buildToolsetTable takes too much time and after few seconds IE 7 shows the prompt that if I want to keep running the script. buildToolsetTable需要太多时间,几秒钟后IE 7会显示提示,如果我想继续运行脚本。 I read that I can use window.setTimeout to make another call and refresh the execution but if someone has implementation then it will be super helpful.
我读到我可以使用window.setTimeout进行另一次调用并刷新执行但是如果某人有实现,那么它将非常有用。
You need something like this which breaks the loop up into a separate function that's called for each iteration: 你需要这样的东西,它将循环分解为一个单独的函数,为每次迭代调用:
(function(n) {
var i = 0;
function doWork() {
// do one row's worth of stuff here
...
if (++i < n) {
setTimeout(doWork, 0);
} else {
// do your tidy up here
...
}
}
doWork(); // start the first iteration
})(rowCount);
Calling setTimeout()
will allow the browser to intersperse UI event handling with your own code. 调用
setTimeout()
将允许浏览器使用您自己的代码散布UI事件处理。
See http://jsfiddle.net/alnitak/8wXTT/ 见http://jsfiddle.net/alnitak/8wXTT/
For added fun, make the single iteration and tidy up functions callbacks, so you can make this code standalone and just pass in the required parameters. 为了增加乐趣,进行单次迭代并整理函数回调,这样您就可以将此代码设置为独立并只传入所需的参数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.