0
我有兩個功能如何讓函數initMap()在它的執行之間等待?
function otherfun(val){
var data = {'latitude': val[0], 'longitude': val[1]};
$.post(URL, data, function(response){
if(response){
// I want to return response from here
}
else{ alert('Error! :('); }
});
}
function initMap() {
var pos = {};
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(function(position) {
var pos = {
lat: position.coords.latitude,
lng: position.coords.longitude
};
var output = otherfun([pos.lat,pos.lng]);
alert(output);
// use output's value further
}
功能initMap()執行最初。我傳遞的緯度和經度值來otherfun()
我想:
- 返回的從功能otherfun響應的價值。
- 使initMap()函數等待otherfun()的返回並存儲在變量輸出中
- 然後顯示帶有輸出值的警報框。