javascript return value is always undefined

前端 未结 3 1859
名媛妹妹
名媛妹妹 2021-01-07 10:50

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 und

3条回答
  •  青春惊慌失措
    2021-01-07 11:30

    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 +"
    Longitude: " + position.coords.longitude; lat=position.coords.latitude; lan=position.coords.longitude; //x.innnerHTML=out alert(lat+"<"+lan); }); return lan; } else alert("error"); }

提交回复
热议问题