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