Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The problem is that you are sending multiple asynchronous requests and the requests are not serialized. Keep in mind that you have no control over the response times of the requests. Also, as the time goes by, the amount of time between the <code>response</code> and the <code>databasecheck</code> invocations keeps increasing. For ex. initially, the <code>databasecheck</code> will be executed after 1.8 seconds and the <code>response</code> will be executed after 2 seconds; the difference is 200 ms. The second time, <code>databasecheck</code> will be executed after 3.6 seconds and the <code>response</code> will be executed after 4 seconds; the difference is 400 ms. </p> <p>Apart from that, the response times of the requests are not under your controls. It is not necessary that the first request you make will necessarily be the first one to be finished. That is, even if you serialize the calls to <code>databasecheck</code> and <code>response</code> functions, if you don't serialize the XHR calls, you will be getting visibly random results. That is, suppose that <code>databasecheck</code> returned <code>x</code> and you sent a request for <code>response1.php</code>; while it is still in progress, suppose a second request for <code>databasecheck</code> returned <code>y</code> and another request for <code>response2.php</code> was initiated. Now depending on the order in which the requests for <code>response1.php</code> and <code>response2.php</code> were finished, you might see a correct or an incorrect result. And, while that is happening, another request for the <code>database.php</code> might be finished and you will once again be seeing some different result. That is the cause for apparent randomness in results.</p> <p>What you need to do is serialize the calls to <code>databasecheck</code> and the <code>response</code> functions. That is, initiate the call to <code>response</code> only after the request for <code>database.php</code> is finished. Now, you need to make sure that the there is no race-condition between the requests as well. That is a little cumbersome and you might lost the real-time behavior.</p> <p>A better way is to combine the results of the calls to <code>database.php</code> and the calls to either <code>response1.php</code> and <code>response2.php</code>. That is, move the logic to the server-side. Put the code in <code>response1.php</code> and <code>response2.php</code> in functions that return values. Suppose these to functions are named <code>response1</code> and <code>response2</code>. In your <code>database.php</code>. call either of these functions based on the database-value. Put the results in an associative array, along with the database value and return the response as a JSON formatted string. This way, you will make one less request and there are no synchronization problems.</p> <p><strong>After Update</strong></p> <p>You need to do the following in your updated code:</p> <pre><code>var requestInProgress = false; function database() { if(requestInProgress) { return; } var xmlhttp = new XMLHttpRequest(); xmlhttp.open("GET","responsedatabase.php",false); xmlhttp.onreadystatechange=function() { if (xmlhttp.readyState == 4) { requestInProgress = false; if(xmlhttp.status==200) { document.getElementById("database").innerHTML=xmlhttp.responseText; } } } requestInProgress = true; xmlhttp.send(); } </code></pre> <p>You could also probably reduce the duration in the <code>setInterval</code> to make sure that there is never much delay between the ending of the previous request and the beginning of a new request.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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