简体   繁体   中英

javascript return value is always undefined

I'm trying to retrieve the 2 attributes of seprated function and I debug there values before the end of the function and they have a value but the return value is alwas undifined I don't know why !!

the .js file

function STAAPlanlat(){
  alert ("the function");

  if (navigator.geolocation) {
    //we supposed to call the handlers of the expections 
    navigator.geolocation.watchPosition(function(position) {
      alert("show position ");
     //  x.innerHTML="Latitude: " + position.coords.latitude +"<br />Longitude: " + position.coords.longitude;  
      var lat=position.coords.latitude;
      var lan=position.coords.longitude;    

      //x.innnerHTML=out
      alert(lat+"<"+lan);
      return lan;
    });

  } else {
    alert("error");
  }
}

I got the alert with the values of the lan and lat

but when I call on separated file it return undefined return value

 <!DOCTYPE html>
 <html>
 <head>
     <script type="text/javascript" src="STAAP1.2.js"> </script>

 <script type="text/javascript">
     function test(){
     var out=STAAPlanlat();     
     document.getElementById("STAAPlanlat").value = "lan is"+out;
     //document.writeln("lan is"+out);
     }
     </script>  
 </head>
 <body>
 <p id="STAAPlanlat">Test the division</p>
 <button onclick="test()">STAAPlanlat()</button>
 <button onClick="alertme()" >Alert</button>

 </body>
 </html>

You are returning in the anonymous function and this value is never assigned to anything. You can do what you want with a callback.

// untested code, hope it works
function STAAPlanlat(callback){
    alert ("the function");
    if (navigator.geolocation) {
        navigator.geolocation.watchPosition(function(position) {
            var lat=position.coords.latitude;
            var lan=position.coords.longitude;  
            callback(lat, lan);
        });
    }
    else{
        alert("error");
    }
}

And your test function...

function test(){
    var out;
    STAAPlanlat(function(lat, lon) { out = lat; });
}

Cause you're not returning it from the main function, you're returning it from the embedded anonymous function which isn't doing anything with it. Do this:

function STAAPlanlat(){
var lat;
var lan;
alert ("the function");
if (navigator.geolocation) {
    //we supposed to call the handlers of the expections 
    navigator.geolocation.watchPosition(function(position) {
        alert("show position ");
        //  x.innerHTML="Latitude: " + position.coords.latitude +"<br />Longitude: " + position.coords.longitude;   
        lat=position.coords.latitude;
        lan=position.coords.longitude;  

        //x.innnerHTML=out
        alert(lat+"<"+lan);
    });
    return lan;
}
else
    alert("error");
}

because function STAAPlanlat doesn't return any value. your anonymous function returns lan but it is asynchronous callback. add this before return lan; :

document.getElementById("STAAPlanlat").value = "lan is" + lan;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM