我在JavaScript中使用navigator.geolocation.watchPosition
,我想要一種方法來處理用戶可能會在watchPosition
找到它的位置之前提交依賴於位置的表單的可能性。等到條件成立爲止?
理想情況下,用戶會定期看到「等待位置」消息,直到獲取位置,然後表單將提交。
但是,由於缺少wait
函數,我不確定如何在JavaScript中實現此功能。
當前代碼:
var current_latlng = null;
function gpsSuccess(pos){
//console.log('gpsSuccess');
if (pos.coords) {
lat = pos.coords.latitude;
lng = pos.coords.longitude;
}
else {
lat = pos.latitude;
lng = pos.longitude;
}
current_latlng = new google.maps.LatLng(lat, lng);
}
watchId = navigator.geolocation.watchPosition(gpsSuccess,
gpsFail, {timeout:5000, maximumAge: 300000});
$('#route-form').submit(function(event) {
// User submits form, we need their location...
while(current_location==null) {
toastMessage('Waiting for your location...');
wait(500); // What should I use instead?
}
// Continue with location found...
});
異步思考。查找'setTimeout'。 –
異步和遞歸可能 - 遞歸調用setTimeout,直到current_latlng有一些值? – Richard
先理解語言再歸咎於它。當然沒有「缺乏」等待功能。 – jAndy