javascript - How to pause in an array after each reaching some count -
i have array of symbols in javascript .
for each element of array , sending server , , doing operation continously every 10
seconds using settimeout .
my requirement , once process half of symbols in array , want give pause operation 3 seconds .
(the size of array fixed 100 , can directly use condition if reached 50 )
i have tried sample program ,
i observed when page loaded first time , affter reaching 2 symbols , ausing 2 seconds . , next iteartion not pauisng .
could please me
<!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title></title> <script src="http://code.jquery.com/jquery-1.10.2.min.js"></script> <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script> <script type="text/javascript"> var symbols = ["one","two","three","four"]; var = 0; function dopoll() { $.each( symbols, function( index, value ){ loadxmldoc(value); }); settimeout(dopoll, 10000); } $(document).ready(function () { dopoll(); }); function loadxmldoc(value) { i++; alert(value); if(i==2) sleep(1000); } function sleep(milliseconds) { var start = new date().gettime(); (var = 0; < 1e7; i++) { if ((new date().gettime() - start) > milliseconds){ break; } } } </script> </head> <body> </body> </html>
in case can iterate on first half of array (with normal for
loop), use settimeout
, iterate on second half of array:
function dopoll() { var middle = math.round(symbols.length / 2); (var = 0, l = middle; < l; i++) { loadxmldoc(symbols[i]); } settimeout(function() { (var = middle, l = symbols.length; < l; i++) { loadxmldoc(symbols[i]); } settimeout(dopoll, 10000); }, 2000); }
Comments
Post a Comment