Jquery 嵌套异步函数
我想从客户端获取地理位置,然后根据Ajax加载位置,然后将它们显示到列表中 我有函数Jquery 嵌套异步函数,jquery,asynchronous,nested,Jquery,Asynchronous,Nested,我想从客户端获取地理位置,然后根据Ajax加载位置,然后将它们显示到列表中 我有函数getGeolocation、loadLocation和createList getGeolocation和loadLocation是异步函数,因此我需要回调或使用延迟对象。我在互联网上搜索了几个小时,但我仍然不理解如何处理这个问题的语法 我知道可以在success函数中调用loadLocations,在afterResponse函数中调用createList,但我想在多个位置调用此函数,因此这不是我的选项 va
getGeolocation
、loadLocation
和createList
getGeolocation
和loadLocation
是异步函数,因此我需要回调或使用延迟对象。我在互联网上搜索了几个小时,但我仍然不理解如何处理这个问题的语法
我知道可以在success
函数中调用loadLocations
,在afterResponse
函数中调用createList
,但我想在多个位置调用此函数,因此这不是我的选项
var lat = 0;
var long = 0;
var locations;
getGeolocation();
loadLocations();
createList();
$('#map').live("pageshow", function() {
google.maps.event.trigger(map, 'resize');
});
function getGeolocation(){
console.log("getGeolocation");
if (navigator.geolocation) {
// getCurrentPosition ruft die Funktion success auf und übermittelt die Position Werte
// error wird ausgeführt wenn es einen Fehler beim ermitteln der Position gibt
navigator.geolocation.getCurrentPosition(success, error);
} else {
alert("GeoLocation API ist NICHT verfügbar!");
}
}
function success(position) {
console.log("success");
lat = position.coords.latitude;
long = position.coords.longitude;
}
function error(msg) {
console.log(typeof msg == 'string' ? msg : "error");
}
function loadLocations(){
console.log("loadLocations");
return $.ajax({
type: "GET",
url: "http://www.example.at/api/getLocationsByGeodata_JSON",
success: afterResponse,
/*beforeSend: showPreloader,*/
data: {lat : lat, long: long},
dataType: 'json'
});
}
function afterResponse(response_objekt) {
console.log("afterResponse");
console.log(response_objekt['results']);
locations = response_objekt['results'];
}
我认为可以使用回调函数来实现这一点 例:
你能解释一下吗`我知道可以在“success”函数中调用“loadLocations”,在“afterResponse”函数中调用“createList”,但我想在许多不同的地方调用这个函数,所以我不选择它'?需要从许多地方调用哪些方法?例如,我还有一个“createMap”函数和一个“showLocation”函数,我想在其中加载具有相同函数的位置。因此,当您从
createMap()
调用loadLocations()
时,您不想调用createList()
,对吗?
var lat = 0;
var long = 0;
var locations;
getGeolocation(function(){
loadLocations().done(function(){
createList();
});
});
$('#map').live("pageshow", function() {
google.maps.event.trigger(map, 'resize');
});
function getGeolocation(successCallback){
console.log("getGeolocation");
if (navigator.geolocation) {
// getCurrentPosition ruft die Funktion success auf und übermittelt die Position Werte
// error wird ausgeführt wenn es einen Fehler beim ermitteln der Position gibt
navigator.geolocation.getCurrentPosition(getCurrentPositionCallback(successCallback), error);
} else {
alert("GeoLocation API ist NICHT verfügbar!");
}
}
function getCurrentPositionCallback(callback){
return function (position) {
console.log("success");
if(typeof callback == 'function'){
callback(position);
}
lat = position.coords.latitude;
long = position.coords.longitude;
};
}
function error(msg) {
console.log(typeof msg == 'string' ? msg : "error");
}
function loadLocations(){
console.log("loadLocations");
return $.ajax({
type: "GET",
url: "http://www.example.at/api/getLocationsByGeodata_JSON",
success: afterResponse,
/*beforeSend: showPreloader,*/
data: {lat : lat, long: long},
dataType: 'json'
});
}
function afterResponse(response_objekt) {
console.log("afterResponse");
console.log(response_objekt['results']);
locations = response_objekt['results'];
}