Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>What you're experiencing here is the difference between asynchronous requests and synchronous JavaScript methodology.</p> <p>The request to Google can take any length of time, and so you find that you have a callback as the second parameter in your <code>geocode()</code> method.</p> <p>The best way to do this is to keep a count of how many responses you've had back, and in the callback method, you can add a function that will then run the rest of the JavaScript that you want to run when you have every response back.</p> <p>There is no way to <em>stop</em> all JavaScript from running, you can merely tell some code to run when it has got every geocode response. </p> <p>I am unsure why you have a <code>setTimeout()</code> in your code also.</p> <pre><code>&lt;script type="text/javascript"&gt; var schools = [ 'Abelsvej 98, 4100, Ringsted', 'Almstoftevej 71, 4100, Ringsted', 'Balstrupvej 35, 4100, Ringsted', 'Ejlstrupvej 101, 4100, Ringsted', 'Ejlstrupvej 101 C 101, 4100, Ringsted', 'Ejlstrupvej 90 90, 4100, Ringsted', 'Haraldsvej 7 7, 4100, Ringsted', 'Løngangen 43 65, 4100, Ringsted', 'Vetterslev Bygade 21 21, 4100, Ringsted', 'Østergade 25, Høm 25, 4100, Ringsted', 'Østergade 3, høm 3, 4100, Ringsted' ]; var count = 0; for (var r = 0; r &lt; schools.length; r++) { (function (r) { var address = schools[r][1]; var geocoder = new google.maps.Geocoder(); setTimeout(function () { geocoder.geocode({ 'address': address }, function (results, status) { if (status == google.maps.GeocoderStatus.OK) { var location = results[0].geometry.location; schoolsArray[r] = location.lat() + ', ' + location.lng(); count++; if(count == schools.length){ doMoreJS(); } } }); } , 1000 * (r + 1)); }(r)); } function doMoreJS(){ // Do more JS } &lt;/script&gt; </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