2014-05-06 81 views
0

我想按順序執行函數,但我經常在console中獲取1,3,4,2。因爲ltt和lott在函數getDistanceFromLatLonInKm中保持爲0。任何想法?提前致謝。Javascript函數順序

var ltt=0; 
var lott=0; 

if (navigator.geolocation){ 
    navigator.geolocation.getCurrentPosition(ajmo); 
    console.log('1'); 
} 

function ajmo(position){ 
    console.log('2'); 
    window.ltt=position.coords.latitude; 
    window.lott=position.coords.longitude; 
    document.write(window.ltt); 
} 

console.log('3'); 
document.write(window.ltt); 
document.write("kurac:" + getDistanceFromLatLonInKm(45.332497,14.436384)); 

function getDistanceFromLatLonInKm(lat1,lon1){ 
    console.log('4'); 
    //second 
    var R = 6371; // Radius of the earth in km 
    var dLat = deg2rad(ltt-lat1); // deg2rad below 
    var dLon = deg2rad(lott-lon1); 
    var a = 
    Math.sin(dLat/2) * Math.sin(dLat/2) + 
    Math.cos(deg2rad(lat1)) * Math.cos(deg2rad(ltt)) * 
    Math.sin(dLon/2) * Math.sin(dLon/2) 
    ; 
    var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a)); 
    var d = R * c; // Distance in km 
    return d; 
} 

function deg2rad(deg) { 
    return deg * (Math.PI/180) 
} 
+1

'geolocation.getCurrentPosition'是異步的,這就是爲什麼3在2和4之前觸發。 – Andy

+0

有沒有辦法讓它同步或在之前觸發? – Axon

+0

JavaScript通常是異步的,所以簡單地編寫代碼並不能確保它的運行方式,您必須使用適當的回調函數。當你使用回調函數時,你要確保它們中的代碼在它們準備運行之前不會運行。 – Jonast92

回答

0

以下更改將確保所有控制檯消息都是爲了1,2,3,4

var ltt=0; 
var lott=0; 

if (navigator.geolocation){ 
    console.log('1'); 
    navigator.geolocation.getCurrentPosition(ajmo); 
} 

function ajmo(position){ 
    console.log('2'); 
    window.ltt=position.coords.latitude; 
    window.lott=position.coords.longitude; 
    document.write(window.ltt); 

    console.log('3'); 
    document.write(window.ltt); 
    document.write("kurac:" + getDistanceFromLatLonInKm(45.332497,14.436384)); 
} 


function getDistanceFromLatLonInKm(lat1,lon1){ 
    console.log('4'); 
    //second 
    var R = 6371; // Radius of the earth in km 
    var dLat = deg2rad(ltt-lat1); // deg2rad below 
    var dLon = deg2rad(lott-lon1); 
    var a = 
    Math.sin(dLat/2) * Math.sin(dLat/2) + 
    Math.cos(deg2rad(lat1)) * Math.cos(deg2rad(ltt)) * 
    Math.sin(dLon/2) * Math.sin(dLon/2) 
    ; 
    var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a)); 
    var d = R * c; // Distance in km 
    return d; 
} 

function deg2rad(deg) { 
    return deg * (Math.PI/180) 
} 
+0

非常感謝。這工作! – Axon

0

你並不需要在這裏使用全局變量。只需在ajmo內包含對getDistanceFromLatLonInKm的調用,這將確保您的代碼按照正確的順序執行操作。

// call getLocation 
getLocation(); 

function getLocation() { 
    if (navigator.geolocation) { 
    navigator.geolocation.getCurrentPosition(ajmo); 
    } 
} 

function ajmo(position) { 
    var lat = position.coords.latitude; 
    var lng = position.coords.longitude; 

    // don't use document.write - it's considered bad practice 
    // this will get access to an element with id="out" 
    var out = document.getElementById('out'); 

    // now pass lat and lng as new parameters into getDistanceFromLatLonInKm 
    out.innerHTML = 'kurac:' + getDistanceFromLatLonInKm(45.332497, 14.436384, lat, lng); 
} 

// now use the lat/lng arguments instead of the global variables 
function getDistanceFromLatLonInKm(lat_origin, lon_origin, lat_pos, lng_pos) { 
    var R = 6371; 
    var dLat = deg2rad(lat_pos - lat_origin); 
    var dLon = deg2rad(lng_pos - lng_origin); 
    var a = 
    Math.sin(dLat/2) * Math.sin(dLat/2) + 
    Math.cos(deg2rad(lat_origin)) * Math.cos(deg2rad(lat_pos)) * 
    Math.sin(dLon/2) * Math.sin(dLon/2) 
    ; 
    var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a)); 
    var d = R * c; 
    return d; 
} 

function deg2rad(deg) { 
    return deg * (Math.PI/180) 
} 
+0

這太好了。謝謝 – Axon

0

之所以他們被稱爲壞了是因爲該方法「geolocation.getCurrentPosition(success, error, options)」是異步的。

https://developer.mozilla.org/en-US/docs/WebAPI/Using_geolocation

的你是給它(函數)對象AJMO實際上是用來形容回調方法。該腳本命中該行並向該函數發送一個異步請求,但它不會等待響應。即使尚未回覆,執行仍繼續進行。其餘的行非常簡單,開銷非常低,所以它們的解析速度比getCurrentPosition方法快得多。 '2'在最後被執行,但這只是因爲方法最終「回調」到您創建的成功函數「AJMO」。

如果你想確保代碼被執行,你可以嘗試這個代替。


var ltt=0; 
var lott=0; 

function getDistanceFromLatLonInKm(lat1,lon1){ 
    console.log('4'); 
    //second 
    var R = 6371; // Radius of the earth in km 
    var dLat = deg2rad(ltt-lat1); // deg2rad below 
    var dLon = deg2rad(lott-lon1); 
    var a = 
    Math.sin(dLat/2) * Math.sin(dLat/2) + 
    Math.cos(deg2rad(lat1)) * Math.cos(deg2rad(ltt)) * 
    Math.sin(dLon/2) * Math.sin(dLon/2) 
    ; 
    var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a)); 
    var d = R * c; // Distance in km 
    return d; 
} 

function ajmo(position){ 
    console.log('2'); 
    window.ltt=position.coords.latitude; 
    window.lott=position.coords.longitude; 
    document.write(window.ltt); 

    console.log('3'); 
    document.write("kurac:" + getDistanceFromLatLonInKm(45.332497,14.436384)); 
} 

if (navigator.geolocation){ 
    navigator.geolocation.getCurrentPosition(ajmo); 
    console.log('1'); 
} 

這使呼叫「2」,「3」和「4」成功的回調方法中,同時還暴露出「getDistanceFromLatLonInKm」作爲一個獨立的功能,如果你需要它用於其他目的。

+0

非常感謝。 有沒有辦法用變量調用getDistanceFromLatLonInKm並且有相同的輸出。因爲在我的例子中,這個函數有硬編碼的參數嗎? – Axon

+0

是不是
document.write(「kurac:」+ getDistanceFromLatLonInKm(window.ltt,window.lott));
MrGoodfix