Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaScript execution hangs page momentarily

I have a web application which uses jQuery/JavaScript heavily. It holds a large array in memory, and the user filters it by typing in a textbox.

Problem: When the filtering algorithm runs, the application becomes non-responsive and the browser may even ask the user whether to let the script continue.

Optimally, I would like the filter function to run in a separate thread, to avoid non-responsiveness. Is this possible in any way? Alternatively, I would like to show a rotating hourglass or similar, but browsers seem unable to display animated GIFs when running heavy scripts.

What is the best way of attacking the problem?

like image 373
Gruber Avatar asked Sep 14 '12 06:09

Gruber


1 Answers

Browsers execute scripts in the main event processing thread. This means any long running scripts can holdup the browser queue.

You should split your filter logic into chunks and run them on timeout callback's. You may use a gap of 0 mills between executions. 0 milli's is just a suggestion to the browser, but the browser will use the gap between subsequent callbacks to clear its event queue. Timeout's is generally how long running scripts are ought to be executed in the browser environment to prevent the page from "hanging".

like image 103
Ashwin Prabhu Avatar answered Oct 22 '22 05:10

Ashwin Prabhu