Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>So that's what you said is called race condition. To avoid it you have two options, either you create an array to store each request, or you create a local variable to store each request. Here's a simple example that avoids the race condition:</p> <pre><code>function customCallBack() {// Ajax Status finished http status OK if( (this.readyState == 4) &amp;&amp; (this.status == 200) ) { alert(this.responseText); } } function anotherCallBack() {// Ajax Status finished http status OK if( (this.readyState == 4) &amp;&amp; (this.status == 200) ) { console.log(this.responseText); } } function asyncPost(url, postArray, functionCallBack) { var request, query; //request must be a local var to avoid race condition query = ''; for (i in postArray)//format post params { query += i + '=' + postArray[i] + '&amp;'; } try {//for modern browsers request = new XMLHttpRequest; } catch (err) {// legacy IE request = new ActiveXObject("Microsoft.XMLHTTP"); } // your custom call back function request.onreadystatechange = functionCallBack; // type url true for async call, false for sync call request.open("POST", url, true); //Header sent to indicate a post form request.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); //making a post call request.send(query); }; //calling asyncPost('yourUrl', {'field1' : 'value1', 'field2': 10}, customCallBack); var post = new Array(); post['field1'] = 'value1'; post['field2'] = 10 //calling againg asyncPost('anotherUrl', post, anotherCallBack); // In jquery is like above, because you need the ajax call in a custom function // and create a local var to hold your call: function jqAjax(url) { var mycall = $.get(url); } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload