Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Because ajax is asynchronous, means once the request is sent it will continue executing without waiting for the response.</p> <p>One easy solution is to turn off the asynchronous nature by passing the 3rd parameter to the <a href="https://developer.mozilla.org/en-US/docs/DOM/XMLHttpRequest#open%28%29" rel="noreferrer">.open()</a> method as false, but it has drawbacks like the browser thread will be blocked till the request is completed means UI will remain unresponsive till the request is completed.</p> <pre><code>xhr.open('GET', 'proxy.php?url='+request_url, false); </code></pre> <p>The correct solution will be is to use a callback method</p> <pre><code>this.requestData = function(latitude, longitude, callback) { request_url = self.API_ENDPOINT + api_key + '/' + latitude + ',' + longitude + '?units=auto'; var xhr = new XMLHttpRequest(); xhr.onreadystatechange = function() { if(xhr.readyState==4 &amp;&amp; xhr.status==200) { content = xhr.responseText; if(content != '' &amp;&amp; (content)) { callback(JSON.parse(content)); } else { callback(false); } } } xhr.open('GET', 'proxy.php?url='+request_url, true); xhr.send(null); } /** * Will return the current conditions * * @param float $latitude * @param float $longitude * @return \ForecastIOConditions|boolean */ this.getCurrentConditions = function(latitude, longitude, callback) { this.requestData(latitude, longitude, function(data) { if(data !== false) { callback(ForecastIOConditions(data.currently)); } else { callback(false); } }); } var forecast = new ForecastIO(api_key); forecast.getCurrentConditions(latitude, longitude, function(condition){ if(condition !== false) { } else { } }); </code></pre>
    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.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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