全局变量没有变化

时间:2015-05-13 09:03:41

标签: javascript

我使用javascript获取当前位置(LngLat) ;并使用全局变量来保存Lng或Lang ..

它在function show();内部发生了变化,但是当它返回到1后,y没有变化。

<!DOCTYPE html>
<html>
<body>

<p>Click the button to get your coordinates.</p>

<button onclick="getLocation()">Try It</button>

<p id="demo"></p>

<script>
var x = document.getElementById("demo");
var y=1;
function getLocation() {
    if (navigator.geolocation) {
    navigator.geolocation.getCurrentPosition(show);
} else { 
    x.innerHTML = "Geolocation is not supported by this browser.";
}
}

function show(p) {
y=p.coords.latitude;
//window.alert(y);
x.innerHTML = "Latitude: " + p.coords.latitude + 
"<br>Longitude: " + p.coords.longitude; 
}

window.alert(y);//<-- here is the problem , I don't want it to alert 1 !

1 个答案:

答案 0 :(得分:0)

看到这一点:

    function show(p) {
    y=p.coords.latitude;
    //window.alert(y);
    x.innerHTML = "Latitude: " + p.coords.latitude + 
    "<br>Longitude: " + p.coords.longitude; 

    startApp();
    }

    function startApp(){
     window.alert(y);
     // do whatever else you want 
    }
相关问题