Setting JavaScript Timeout limit in Google Chrome

后端 未结 3 1982
不知归路
不知归路 2021-01-02 12:35

Is it possible to increase the time out limit for JavaScript?

If I have a script that executes for more than 20/30 seconds chrome will pop-up with the unresponsable

3条回答
  •  悲哀的现实
    2021-01-02 13:02

    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: 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();
    }
    

    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);
    

提交回复
热议问题