2012-07-11 123 views
1

我試圖檢索分離函數的2個屬性,我調試那裏的值在函數結束之前,他們有一個值,但返回值alwas未定義我不知道爲什麼!javascript的返回值總是未定義

的js文件

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"); 
    } 
} 

我得到了警報,局域網的值和緯度

但是當我呼籲分離文件時,它返回undefined返回值

<!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> 

回答

2

因爲你沒有從主函數返回它,所以你從嵌入的匿名函數返回它,它沒有做任何事情。這樣做:

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"); 
} 
+0

非常感謝,現在的工作,很高興你:) @jeschafe – 2012-07-11 21:50:23

+0

這個答案幫助比其他任何我多,謝謝大家!我沒有意識到從嵌入函數返回值,在這種情況下,ajax成功函數並沒有將它返回到主函數以供在外部使用。 – 2018-02-07 11:08:17

0

因爲函數STAAPlanlat不返回任何值。您的匿名函數返回lan,但它是異步回調。 return lan;前補充一點:

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

非常感謝:)我會用它來測試值:)謝謝 – 2012-07-11 21:50:59

3

您在匿名函數返回並且這個值是從來沒有分配到任何東西。你可以用回調做你想做的事。

// 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"); 
    } 
} 

而且你的測試功能...

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

非常感謝:)它適用於我,但我有一個旅行時使用'/ /警報(「出是」+出); \t document.writeln(「lan is」+ out);'它是未定義的,但是當我發出警報時,它返回值'alert(「out is」+ out); \t document.writeln(「lan is」+ out);'''我認爲我需要延遲計時器,非常感謝很多:) @iurisilvio – 2012-07-11 21:47:10

+1

時間延遲不是一個好主意。你應該準備一個回調函數,它用lat和lng來做你想要的。即 'STAAPlanlat(函數(lat,lon){alert('lat ='lat); document.write('lng ='+ lon)});' – jagm 2012-07-12 07:11:48

+0

準確地說,你可以用定時器來做,但它是不是一個好主意。改變我在代碼中定義的回調來完成你所需要的,就像@jagm所說的那樣。 – iurisilvio 2012-07-12 12:29:40