Unresponsive script - can this be avoided?

I have a list of 150 lines, and for each line there are three selected items.

Since there is a lot of processing before displaying each result, I get an error: "A script on this page may be busy or may stop responding. Now you can stop the script, or you can continue to see if the script ends." and this applies to jquery.js file.

Can this error be avoided by doing some jQuery work?

Thanks.

+6
source share
4 answers

You need to divide the processing into several parts and give the browser some time to do something other than them, using 0 or 1 ms setTimeout.

A very simple method will use the forEachSeries async method:

 async.forEachSeries(yourData, function(item, cb) { // process item async.nextTick(cb); }); 

yourData can be a jQuery object containing your rows, then item will be a single row DOM element.

+6
source

I do not know about jQuery (if there is some kind of shell in jQuery), but in vanilla JS there is the concept of WebWorkers (support is pretty good, except for IE - see here ).

Here you run an additional thread for your calculations that does not block your user interface thread. When the calculations are done, and only the display of the results remains, you send the data from the worker to the user interface stream and simply show the results.

See the MDN tutorial for more information.

0
source

Download JavaScript as needed, rather than loading it all on the download page.

JavaScript Required

0
source

Website guru Steve Suders has written about this issue and how to solve it in his book, Even Faster Websites. It describes a solution in which you use timeouts so that other processes get some time.

Here is a part of the book devoted to these issues.

0
source

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


All Articles