Forums
New posts
Search forums
Members
Current visitors
Log in
Register
What's new
Search
Search
Search titles only
By:
New posts
Search forums
Menu
Log in
Register
Install the app
Install
Forums
Archive
Archive
Javascript
browser doesn't response, long calculations
JavaScript is disabled. For a better experience, please enable JavaScript in your browser before proceeding.
You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an
alternative browser
.
Reply to thread
Message
[QUOTE="Thomas 'PointedEars' Lahn, post: 5008422"] It's _millisecond_, whether it is a long or short time for a browser depends on several factors. Your main problem, however, is that your approach is wrong. By calling setTimeout() in a loop with the same milliseconds value, you are running "threads" (for the lack of a better word) at approximately the same time. This is why the browser becomes unresponsive but does not consider the script to hang. Probably the CPU usage of the browser or the tab process goes through the roof: CPU usage (%) ^ ,-------------. 100 + | Thread #100 | : `~~~~~~~~~~~~~' : ,~~~~~~~~~~~~~. : | Thread #2 | : :-------------: : | Thread #1 | '----+----------------> time (ms) 100 While you could run window.setTimeout() (you should use the qualified reference) with a larger multiple of the loop index as milliseconds value, this would not solve the problem as threads could still become stacked if a thread took more time than you defined: CPU usage (%) ^ 100 + ,. ,. : #4|| #5|| … : ,----.-----.----.----. : | #2 | #3 | #4 | #5 | … : ,---------+-----+----+----+----. : | #1 | #2 |#3 | #4 | #5 | … '----+----+----+----+----+----+----+----+----> time (ms) 100 200 300 400 500 600 So you should only run *one* "thread" that does the calculation instead: window.setTimeout(function() { for (j = 0; j < 100; j++) { /* heavy calculations */ } }, 10); which would mean CPU usage (%) ^ 100 + : : : : ,----------------------------------------. : | Thread #1: 1 … 100 | '-+------------------------------------------> time (ms) 10 See also my other answer. HTH. PointedEars [/QUOTE]
Verification
Post reply
Forums
Archive
Archive
Javascript
browser doesn't response, long calculations
Top