Note that there are some explanatory texts on larger screens.

plurals
  1. POwhy my program first executes function addmarkers() - not addAddresstoMap()
    primarykey
    data
    text
    <p>I have a function LoadXML() in which after a ajax call to the server I've got an array with 100 addresses type string; </p> <p>Also here we call the function extractAddressed()with the function <code>addmarkers</code> as callback;</p> <pre><code>function loadXMLDoc(l) { //making ajax call to the server to get the addresses arrayMyAddresses.push(rec[i][8] + "," + rec[i][10]); extractAddresses(addmarkerss); } </code></pre> <p>Here is <code>extractAddresses</code> function which send a part of the addresses which i want to geocode using the <code>getLocations</code> method which performs asynchronous call to the google servers </p> <pre><code>function extractAddresses(callback) { alert (startman); i=startman; while (i&lt;startman+intPageSize) { geocoder = new GClientGeocoder(); //alert(arrayMyAddresses[i]); geocoder.getLocations(arrayMyAddresses[i], addAddressToMap); i++; } //callback(); setTimeout(callback,300); } </code></pre> <p>Here you may also see the function <code>addAddresstoMap</code> which handle the response from google servers and creates an array of gmarkers!</p> <pre><code>function addAddressToMap(response) { if (!response || response.Status.code != 200) { alert("Sorry, we were unable to geocode that address"); } else { place = response.Placemark[0]; point = new GLatLng(place.Point.coordinates[1], place.Point.coordinates[0]); var marker = new GMarker(point); batch.push(marker); //alert ("batchaddaddresstomap " + batch.length); } } </code></pre> <p>And at last <code>addmarkerss</code>() function which add the array of Gmarkers to the map;</p> <pre><code>function addmarkerss() { // alert("batchf: " + batch.length); mgr = new MarkerManager(map); mgr.addMarkers(batch,13); mgr.refresh(); } </code></pre> <p>So this is how the programme should work when i call <code>extractAddresses</code> and i give one by one the addresses for geocoding to <code>addAddresstoMap</code> function and they are dynamically added to an array of type Gmarker; when I'm ready with this i call addmarkers() to put the markers on my map;</p> <p><strong>But what does it make actually</strong> </p> <p>when i call <code>extractaddresses</code> it somehow goes first to <code>addmarkers</code> function it sees that <code>banch</code> array is empty and it doesn't load any markers; When i put <code>setTimeout(callback,300);</code>and i postpone the execution of <code>addmarkers</code> everything works.</p> <hr> <p><strong>What may be the reason. And i also want to ask if the problem is here: Although it is true that a callback function will execute last if it is placed last in the function, this will not always appear to happen. For example, if the function included some kind of asynchronous execution (*<em>like an Ajax call as it's my case</strong>), <strong>then the callback would execute after the asynchronous action begins, but possibly before it finishes</strong>. is it my case? also how can i solve my problem without <code>settimeout</code></em>* </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.
    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