有一个函数获取当前城市的值:
function getLoaction(){
if (navigator.geolocation)
{
navigator.geolocation.getCurrentPosition(showPosition, showErr);
}
function showPosition(){
$.getJSON(url,function(data){
var yourCurCity = data.city;
})
}
然后在另一个函数中调用yourCurCity的值
function getWeather(){
console.log(yourCurCity);//显示为undefine
$.ajax(url,{
data:{"city":yourCurCity},function(data){
///....
}
})
}
在getWheather函数中怎样调用showPosition 的city值
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
将
yourCurCity声明在外部或者在getJSON成功后调用getWeather时传参。如果getWheather不是异步的话。只能采用后者,或者是进行promise回调。异步获取的需要用回调函数 推荐
Promise写法 避免回调金字塔