javascript for loop not executing function until end of loop, then iterating function n number of times -


i attempting load div's google map images loop adds 1 value of document.get. problem getlocation() call comes @ final iteration of loop, iterates loops n number of times.

for(var = 1; < length; i++){ var longitude = 1.8612393999999999; var latitude = 50.7263312; document.getelementbyid("longinput").value = longitude; document.getelementbyid("latinput").value = latitude;  var mapholder = "mapholderm"+i; document.getelementbyid("mapholdervalue").value = mapholder; themapholder=document.getelementbyid(mapholder);  getlocation(); } 

getlocation() uses longinput , latinput values lat , lon variables, , themapholder place put target div image. maps able co ordinates , explaned below on final, or end of loop themapholder value.

i think not issue getlocation(), , more how attempting use it. adding alert in getlocation() call , viewing elements on screen know elements populated though loop , change, location called n number of times after happens. getlocation() picks values set iterations of giving element values complete.

adding alert @ beginning of loop makes loop go round n number of times execute getlocation() once.

just sum up: loops , changes element values, 1 one. once complete getlocation() called, same number of iterations. getlocation() can access final setting of elements gives 1 div map.

dean

update: sorry have given incorrect information: appear method call loop results in same behaviour:

function pulse(){      $('#contentdiv').delay(200).fadeout('slow').delay(50).fadein('slow');     } 

calling method loop:

for(var = 1; < length; i++) {             var longitude = 1.8612393999999999;     var latitude = 50.7263312;      alert("end of loop");             pulse(); } 

results in alert n number of times, pulse executed? sorry confusion.

dean

update: google maps script:

function getlocation()   {   if (navigator.geolocation)     {     navigator.geolocation.getcurrentposition(showposition,showerror);     }   else{x.innerhtml="geolocation not supported browser.";}   }  function showposition(position)   {   alert($("#longinput").val()); */   lat= $("#latinput").val();   lon= $("#longinput").val();   latlon=new google.maps.latlng(lat, lon);   mapholder=themapholder;    alert("getlocation " + themapholder);    var myoptions={   center:latlon,zoom:14,   maptypeid:google.maps.maptypeid.roadmap,   maptypecontrol:false,   navigationcontroloptions:{style:google.maps.navigationcontrolstyle.small}   };   var map=new google.maps.map(themapholder,myoptions);   var marker=new google.maps.marker({position:latlon,map:map,title:"you here!"});   }    function showerror(error)   {   switch(error.code)      {     case error.permission_denied:       x.innerhtml="user denied request geolocation."       break;     case error.position_unavailable:       x.innerhtml="location information unavailable."       break;     case error.timeout:       x.innerhtml="the request user location timed out."       break;     case error.unknown_error:       x.innerhtml="an unknown error occurred."       break;     }   } </script> 

using alert in showposition can see called after loop has updated elements.

why function behave this, , called after loop has executed else?

as aware reads little confusing, due needing learn more , javascript although see in comments there adivice learn me. how solution problem:

getlocation() being itterated, after loop had completed iterating on other code. hack, when getlocation() method iterated time make element value changes function use:

  var y=1;   var x=document.getelementbyid("demo");    function getlocation()   {   if (navigator.geolocation)     {     navigator.geolocation.getcurrentposition(showposition,showerror);     }   else{x.innerhtml="geolocation not supported browser.";}   }    function showposition(position)   {     var longitude = -1.8612393999999999;     var latitude = 50.7263312;      document.getelementbyid("longinput").value = longitude;     document.getelementbyid("latinput").value = latitude;      var mapholder = "mapholderm"+y;     document.getelementbyid("mapholdervalue").value = mapholder;     themapholder=document.getelementbyid(mapholder);      lat= $("#latinput").val();        lon= $("#longinput").val();     latlon=new google.maps.latlng(lat, lon);     mapholder=themapholder;      alert("getlocation " + themapholder+y);     y++;      var myoptions={       center:latlon,zoom:14,       maptypeid:google.maps.maptypeid.roadmap,       maptypecontrol:false,       navigationcontroloptions:{style:google.maps.navigationcontrolstyle.small}     };    var map=new google.maps.map(themapholder,myoptions);   var marker=new google.maps.marker({position:latlon,map:map,title:"you here!"}); } 

notice introduction of variable y increments in value each time method called use in element id.


Comments

Popular posts from this blog

java - Jmockit String final length method mocking Issue -

asp.net - Razor Page Hosted on IIS 6 Fails Every Morning -

c++ - wxwidget compiling on windows command prompt -