Methods not completing before the next one is executed (javascript jquery google maps v3 api) -


i have been trying place multiple map markers lat lng coords in xml file.

this code

    var stockistname=[];         var stockistaddress=[];         var stockistlat=[];         var stockistlng=[];       function getaddresses(country){           $.get('xml/stockists/'+country+'.xml', function(d){                       alert("file opened");          $(d).find('stockist').each(function(){             $stockist = $(this);             stockistname.push($stockist.attr("name"));                   stockistaddress.push($stockist.attr("address"));             stockistlat.push($stockist.attr("lat"));             stockistlng.push($stockist.attr("lng"));             });         });     placemarkerarray(); } function placemarkerarray(){          alert("this element 0"+stockistname[0]);         for(var i=0; i<stockistname.length;i++){             alert("in function got addresses");         //alert("inloop"+i+"");          var newlatlng = new google.maps.latlng(stockistlat[i], stockistlng[i]);         //alert("making marker lat lng:"+newlatlng+"")             if (markerarray[i] != undefined){                 markerarray[i].setposition(newlatlng);              }             else{                 markerarray[i] = new google.maps.marker({                     position: newlatlng,                     map: map,                     icon:'images/icons/map/van.png'                 });             }         } }` 

i have been reading callback functions can't seem grasp idea. thought things executed in order write them. if give me direction great.

i think things clearest example, so:

// define function accepts function parameter , calls // function after 1 second asynchronously. function executeactiondelayed (action) {     window.settimeout(function () {         action();         }, 1000); }  // call above function anonymous function parameter. executeactiondelayed(function () {     console.log("execute action delayed called."); });  console.log("end, or it?"); 

as can see in example, "end, or it?" logged console before "execute action delayed called.". because window.settimeout asynchronous. function created executeactiondelayed using callback!

this similar you're doing jquery's get function:

// function being passed in parameter , // called when response has been received. $.get('xml/stockists/'+country+'.xml', function(d){     // stuff removed. }); 

so mean you?

you have call function uses data within success callback pass in:

function getaddresses(country){      $.get('xml/stockists/'+country+'.xml', function(d){         alert("file opened");         $(d).find('stockist').each(function(){             $stockist = $(this);             stockistname.push($stockist.attr("name"));                   stockistaddress.push($stockist.attr("address"));             stockistlat.push($stockist.attr("lat"));             stockistlng.push($stockist.attr("lng"));         });         placemarkerarray();     }); } 

Comments