Setting JavaScript JavaScript timeout limits

Is it possible to increase the time limit for JavaScript?

If I have a script that runs more than 20/30 seconds, chrome will pop up with an unanswered dialog box.

Creating a more efficient script won't help me, because the script sometimes needs to iterate through a function a million or a billion times

+6
source share
3 answers

To break the function into steps / pieces and run them inside setInterval(function(){}) . This page will be receptive, you will be able to notify the user of the progress, and you will get your work.

UPDATE Here is the simplest function that a worker takes, executing each iteration, chunksz - the number of iterations performed in one maxit block - the 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 can see, you are ordering everything in order.

UPDATE2

Let's say you have a loop:

  for(var i=0; i<100000; ++i) { ... do something ... } 

then you need to wrap the loop body in a function and call the task above with it as follows:

 task(function(i){ ... do something ... },100, 100000); 

or like this:

 function loopBody(i){ ... do something ... } task(loopBody,100, 100000); 
+5
source

When you have a lot of processing for the client side, you need to divide your work into separate threads. There is only one thread in the browser for processing user input (events) and for processing JS. If you process too much JS without yielding, the user interface becomes irrelevant and the browser is not satisfied.

How can you let get a script? A new way is to use web workers http://www.whatwg.org/specs/web-workers/current-work/ . This works by creating a separate thread to run JS, the thread thread does not have access to the DOM and can be run simultaneously.

However, this new technology does not exist in all browsers. For older browsers, you can split your work by getting a script call through timeouts. Whenever a timeout occurs, the script is inferior to the browser to trigger its events, as soon as the browser is executed, your next timeout will be launched.

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

In the jsfiddle example, a synchronous call is issued, you can return it to see how the browser goes around. Please note that it takes a long time to complete the asynchronous call, but this does not lead to a long script message, and it allows you to interact with the page when calculating (select the text, button hang effects). You can see that it prints partial results in the panel on the bottom right.

UPDATE http://jsfiddle.net/mendesjuan/PucXf/8/

Try using the c-smile task function to implement the sum of squares. I think that it does not have a parameter, a function to call when the task is completed. Using task allows us to create several functions without duplicating the work of calling setTimeout and iteration.

 /** * @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, list, 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); }) 
+3
source

If your goal is to suppress the β€œKill-Wait” message as a quick temporary fix for your slow JavaScript, then the solution is to open Developer Tools / Tools in Google Chrome and keep it open and minimize it somewhere on your desktop table while watching.

+2
source

Source: https://habr.com/ru/post/890444/


All Articles