Note that there are some explanatory texts on larger screens.

plurals
  1. POJavascript Callback Within a Callback
    primarykey
    data
    text
    <p>I am a stuck as to why I never hit the <code>GETNEARCALLBACK</code> function below. The logic goes something like this:</p> <ol> <li>On Page Load I call <code>INITIALIZE</code></li> <li><code>INITIALIZE</code> happily executes and calls <code>GETSTATIONS</code></li> <li><code>GETSTATIONS</code> does an AJAX request using <code>GETNEARESTSTAIONS</code> as the callback function, and the web server responds with the results of a database query in JSON format.</li> <li><code>GETNEARESTSTAIONS</code> takes the results and creates a Google Maps API distance matrix request using <code>GETNEARCALLBACK</code> as the callback function</li> <li>I run my site and use Firebug to determine that I never get to <code>GETNEARCALLBACK</code>. </li> </ol> <p>I think my use of Google Maps API is correct because if I don't call <code>GETNEARESTSTATIONS</code> from within my AJAX request, it executes properly.</p> <p>function INITIALIZE() { GETPOSITION(); DRAWMAP(); GETADDR(); GETSTATIONS(); }</p> <pre><code>var xmlhttp; function GETSTATIONS() { if(window.XMLHttpRequest) { xmlhttp = new XMLHttpRequest(); } else { xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); } xmlhttp.onreadystatechange = GETNEARESTSTATION(); xmlhttp.open("GET", "final.php", true); xmlhttp.send(); } var STATIONLIST; function GETNEARESTSTATION() { if(xmlhttp.readyState == 4 &amp;&amp; xmlhttp.status == 200) { STATIONLIST = eval("(" + xmlhttp.responseText + ")"); var LAT = parseFloat(document.getElementById("LATITUDE").value); var LON = parseFloat(document.getElementById("LONGITUDE").value); var latlng = new google.maps.LatLng(LAT, LON); var destinationA = STATIONLIST[0].ADDRESS; var service = new google.maps.DistanceMatrixService(); service.getDistanceMatrix({ origins: [latlng], destinations: [destinationA], travelMode: google.maps.TravelMode.DRIVING, unitSystem: google.maps.UnitSystem.IMPERIAL, avoidHighways: false, avoidTolls: false }, GETNEARCALLBACK); } } function GETNEARCALLBACK(response, status) { if(status == google.maps.DistanceMatrixStatus.OK) { var destinations = response.destinationAddresses; var results = response.rows[0].elements; for(var j = 0; j &lt; results.length; j++) { var element = results[j]; document.getElementById("STATIONADDR").innerHTML = parseFloat(element.distance.value) + " " + response.destinationAddresses[j]; } } } </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.
 

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