javascript - Why Does The Browser Hang When Updating The Progress Bar? -
problem: have code runs , needs periodically update progress bar. seems work fine until part of code update occurs inside loop.
after hangup, progress bar update whatever supposed after loop. instance in code:
<!doctype html> <html> <head> <title>test</title> <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script> <script> $(document).ready(function(){ $('#button1').click(function(){ $("#progressbar").val(0); //reset 0 for(var = 0; < 100000; i++){ $("#progressbar").val(i/1000); //if(i === 50000){ // alert("half way there"); //} } }); }); </script> </head> <body> <button id="button1">simulate progress</button> <progress id="progressbar" value="0" max="100"></progress> </body> </html>
my browser (firefox) hang-up once button pressed , after couple of seconds display 100% complete progress bar. if uncomment code, see progress bar 50% complete when alert shows up.
why hang , not produce smooth progress update? tried playing around web workers, saw pretty same response.
javascript runs in single thread. keeping busy looping doesn't have time go off , repaint page.
use iterator settimeout
or setinterval
instead.
Comments
Post a Comment