javascript - One XHR Request at a time after completion of current request -
i working on program checks if user id exists sending user id post method processid.php using xhr request. first tried xhr requests in loop in array of ids killed browser consist of thousands of elements because thousands of xhr requests made @ time. found post on stackoverflow said should not using xhr request in loop instead should call function within funtion wrote following code. program seems work kills browser because still thousands of request made.
i trying figure out week how make single xhr request @ time after completing current/in-process xhr request. help? solution can create on complete function this.
ps: don't want use jquery.
<?php $php_array_id = [element-1,element-2.........,element-n]; $js_array_id = json_encode($php_array); echo "var userids = ". $js_array_id . ";\n"; $url = "http://example.com/idprocess.php"; ?> <script> function processnext(i) { if (i < userids.length) { var http = new xmlhttprequest(); var url = "<?php echo $url ?>"; var params = "id=" + userids[i]; http.open("post", url, true); http.onreadystatechange = function() { if(this.readystate == 4 && this.status == 200) { if(this.responsetext){ document.getelementbyid("reports").innerhtml += "<div>id = " + this.responsetext + " exists</div>"; } } } http.send(params); i++; processnext(i); } } processnext(0); </script>
try this
<script> var userids = <?php echo($js_array_id) ?>; function processnext(i) { var http = new xmlhttprequest(); var url = "<?php echo($url) ?>"; var params = "id=" + userids[i]; http.open("post", url, true); http.onreadystatechange = function() { if(this.readystate == 4 && this.status == 200) { if(this.responsetext){ document.getelementbyid("reports").innerhtml += "<div>id = " + this.responsetext + " exists</div>"; i++; if(i<userids.length){processnext(i);} } } } http.send(params); } processnext(0);
Comments
Post a Comment