Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unresponsive Javascript checking lots of elements [duplicate]

Tags:

javascript

I created a brute-force like script which basically needs to check more than 27,000 options, and after each check displays the result inside a div.

The script is coded correctly and if I lower the number of options it works sufficiently well, but if I have many options, after a few seconds, a window pops up telling me that my script is unresponsive. How can I make it responsive while checking this many options.

Oh and I almost forgot, it displays data (which is displayed after every check) only when that pop-up window appears (kinda weird).

like image 311
AleksaIl Avatar asked Nov 13 '22 05:11

AleksaIl


1 Answers

Asynchronous batch processing may solve your problem:

var options = ...; // your code

// I assume you are using something like this
function processAll() {
  for(var i=0; i<options.length; ++i) ... // causes unresponsivity
}

// try to use this instead
function batchProcessing(from) {
  if(from >= options.length) return;
  var to = Math.min(1000, options.length-from);
  for(var i=from; i<from+to; ++i) ... // your code
  // run the next batch asynchronously, let the browser catch the breath
  setTimeout(batchProcessing.bind(null, from+1000));
}
like image 187
Jan Turoň Avatar answered Nov 15 '22 05:11

Jan Turoň