javascript - How to make a sleep function? -
in while loop of http requests when sending http request want next request wait moment let me response previous one. settimeout function ?
here code :
while(i <= 50){             http.onreadystatechange = function(){                            if(http.readystate == 4){                         getposts(http.responsetext , i);                         //alert(http.responsetext);                     }             }             http.open("get","../php/php.php?q=getbody&id=comment"+i);                    http.send();             //sleep(0); function in send request php file make asleep              i++;         }   also here photo happen in firebug: firebug error photo
you delay execution of second method via settimeout, however, not best way in javascript.
when response server comes, can execute callback. callback should handle response , send next request.
it common use library handle ajax , i'm sure 1 use has plenty of documentation on issue of callbacks started. alternatively, in plain js, can onreadystatechange attribute of xhr object callback.
https://developer.mozilla.org/en-us/docs/web/api/xmlhttprequest
Comments
Post a Comment