在Google Chrome中设置JavaScript超时限制 [英] Setting JavaScript Timeout limit in Google Chrome
问题描述
是否有可能增加JavaScript的超时限制?
Is it possible to increase the time out limit for JavaScript?
如果我有一个执行超过20/30秒的脚本,Chrome会弹出没有响应的页面对话框。
If I have a script that executes for more than 20/30 seconds chrome will pop-up with the unresponsable page dialog.
制作更高效的脚本不会对我有所帮助,因为脚本有时需要遍历一百万或十亿次功能
Making a more efficient script won't help me because the script sometimes need to iterate through a function for a million or billion times
推荐答案
在步骤/块上拆分函数并运行 setInterval(function(){})
。
这种方式的页面会很快响应,您将能够通知用户执行进度,并完成工作。
To split the function on steps/chunks and run those inside setInterval(function(){})
.
This way page will be responsive, you will be able to notify user about progress of execution and you will get your job done.
UPDATE :这是一个简单的函数,它执行每次迭代
worker
函数,
chunksz
- 在单个块中运行的迭代次数
maxit
- 迭代总次数
UPDATE: Here is simple function that takes
worker
function executing each iteration,
chunksz
- number of iteration running in single chunk
maxit
- total number of iterations.
function task(worker, chunksz, maxit)
{
var idx = 0;
var xint = null;
function exec_chunk()
{
for(var n = 0; n < chunksz; ++n)
{
if(idx >= maxit) { return; }
worker(idx++);
}
setTimeout(exec_chunk,1);
}
exec_chunk();
}
下面是一个例子: http://jsfiddle.net/Ed9wL/
正如你所看到的,你可以按顺序获得所有迭代。
Here is an example : http://jsfiddle.net/Ed9wL/ As you see you get all iterations in order.
UPDATE2 :
假设您有一个循环:
Say you have a loop:
for(var i=0; i<100000; ++i) { ... do something ... }
然后您需要将循环的主体封装到一个函数中,并像这样调用上面的任务
:
then you need to wrap body of the loop into a function and call the task
above with it like this:
task(function(i){ ... do something ... },100, 100000);
或者像这样:
or like this:
function loopBody(i){ ... do something ... }
task(loopBody,100, 100000);
这篇关于在Google Chrome中设置JavaScript超时限制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!