微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

在Google Chrome中设置JavaScript超时限制

如何解决在Google Chrome中设置JavaScript超时限制

| 是否可以增加JavaScript的超时限制? 如果我有一个执行时间超过20/30秒的脚本,chrome将弹出带有不负责任的页面对话框的对话框。 制作效率更高的脚本对我无济于事,因为有时脚本需要迭代一百万或十亿次     

解决方法

        要在步/块上拆分功能并在
setInterval(function(){})
中运行它们。 这样,页面将具有响应性,您将能够通知用户执行进度,从而完成工作。 更新:这是需要的简单函数 each1ѭ函数执行每次迭代,
chunksz
-单个块中运行的迭代数
maxit
-迭代总数。
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/ 如您所见,您将按顺序获得所有迭代。 UPDATE2: 假设您有一个循环:
 for(var i=0; i<100000; ++i) { ... do something ... }
那么您需要将循环的主体包装到一个函数中,并像这样调用上面的
task
task(function(i){ ... do something ... },100,100000);
或像这样:
function loopBody(i){ ... do something ... }
task(loopBody,100000);
    ,        当您有很多处理工作要在客户端进行时,您需要将工作拆分为单独的线程。浏览器只有一个线程来处理用户输入(事件)和处理JS。如果您处理过多的JS,却没有让步,则UI会变得无响应,并且浏览器会不满意。 如何让脚本产生效果?新方法是使用网络工作者http://www.whatwg.org/specs/web-workers/current-work/。这是通过创建单独的线程来运行JS来实现的,线程线程无法访问DOM,并且可以并发运行。 但是,并非所有浏览器都具有这种较新的技术。对于较旧的浏览器,您可以通过使脚本通过超时自行调用来分散工作。每当发生超时时,脚本就会让浏览器运行其事件,一旦浏览器完成,就会触发您的下一个超时。 示例http://jsfiddle.net/mendesjuan/PucXf/
var list = [];
for (var i = 0; i < 500000; i++) {
   list.push(Math.random());
}


function sumOfSquares(list) {  
  var total = 0;
  for (var i = 0; i < list.length; i++) {
      total += list[i] * list[i];
      // DOM manipulation to make it take longer
      var node = document.createElement(\"div\");
      node.innerHTML = \"Sync temp value = \" + total;
      document.body.appendChild(node);
  }
    return total;
}


function sumOfSquaresAsync(arr,callback) {
  var chunkSize = 1000; // Do 1000 at a time
  var arrLen = arr.length;
  var index = 0;
  var total = 0;  

  nextStep();

  function nextStep() {
     var step = 0;
     while (step < chunkSize && index < arrLen) {
       total += arr[index] * arr[index];
       // DOM manipulation to make it take longer
       var node = document.createElement(\"div\");
       node.innerHTML = \"Async temp value = \" + total;
       document.body.appendChild(node);         
       index++;
       step++;
     }

     if (index < arrLen) {
        setTimeout(nextStep,10);
     } else {
       callback(total);
     }
  }
}



sumOfSquaresAsync(list,function(total) {console.log(\"Async Result: \" + total)});

//console.log(\"Sync result\" + sumOfSquares(list));
jsfiddle上的示例已注释掉同步调用,您可以将其放回去以查看浏览器进行爬网。请注意,异步调用确实需要很长时间才能完成,但是不会导致脚本运行时间长,并且可以让您在计算时与页面进行交互(选择文本,按钮悬停效果)。您可以在右下方的窗格中看到它打印了部分结果。 更新http://jsfiddle.net/mendesjuan/PucXf/8/ 让我们尝试使用c-smile的task函数来实现平方和。我认为他缺少一个参数,一个在任务完成时要回调的函数。使用
task
允许我们创建多个分块函数,而无需重复调用setTimeout和迭代的工作。
/**
 * @param {function} worker. It is passed two values,the current array index,*        and the item at that index
 * @param {array} list Items to be traversed
 * @param {callback} The function to call when iteration is finished;
 * @param {number} maxit The number of iterations of the loop to run 
 *        before yielding,defaults to 1000
 */
function task(worker,list,callback,maxit)
{
  maxit = maxit || 1000; 
  var idx = 0;
  exec_chunk();
  function exec_chunk() 
  {
     for(var n = 0; n < maxit; ++n)
     {
       if(idx >= list.length) {
         callback(); 
         return;
       }
       worker(idx,list[idx]);
       idx++;
     }
     setTimeout(exec_chunk,1);
  }
}


function sumOfSquaresAsync(list,callback) 
{
   var total = 0;

   // The function that does the adding and squaring
   function squareAndAdd(index,item) {
      total += item * item;
      // DOM manipulation to make it take longer and to see progress
      var node = document.createElement(\"div\");
      node.innerHTML = \"Async temp value = \" + total;
      document.body.appendChild(node);                
   }

   // Let the caller know what the result is when iteration is finished
   function onFinish() {
      callback(total);
   }

   task(squareAndAdd,onFinish);

}

var list = [];
for (var i = 0; i < 100000; i++) {
   list.push(Math.random());
}

sumOfSquaresAsync(list,function(total) {
    console.log(\"Sum of Squares is \" + total);        
})
    ,        如果您的目标是将\“ Kill-Wait \”消息作为对慢速JavaScript的快速临时修复,那么解决方案是在Google Chrome中打开“工具/开发者工具”,并在浏览时在桌面上的某个位置保持打开和最小化。     

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。