Javascript 如何在jquery中全局访问callbak函数变量

Javascript 如何在jquery中全局访问callbak函数变量,javascript,php,jquery,Javascript,Php,Jquery,在此代码中: $(document).ready(function() { var lat = 0; var lng = 0; function getLatLng(address) { var geocoder = new google.maps.Geocoder(); geocoder.geocode({'address': address}, function(results, status)

在此代码中:

$(document).ready(function() {
        var lat = 0;
        var lng = 0;
        function getLatLng(address) {
            var geocoder = new google.maps.Geocoder();
            geocoder.geocode({'address': address}, function(results, status) {
                if (status == google.maps.GeocoderStatus.OK) {
                    lat = results[0].geometry.location.lat(); //how do I access lat 
                    lng = results[0].geometry.location.lng() //and lng outside function ormake it global
                }
            });
            alert(lat); // does not display only show's the 0
        }
        getLatLng();
    });
我希望
警报(lat)
显示lat不为零

我怎样才能访问这个

提前谢谢

这是因为
警报(lat)在成功函数之前执行。您应该在回调函数内发出警报,或者使用
setTimeout
发出警报

使用setTimeout是一种糟糕的做法,因为我们永远不知道执行服务器端调用需要多长时间。因此,更好的做法是在回调函数中调用代码以确保更改

这是因为
警报(lat)在成功函数之前执行。您应该在回调函数内发出警报,或者使用
setTimeout
发出警报


使用setTimeout是一种糟糕的做法,因为我们永远不知道执行服务器端调用需要多长时间。因此,最好在回调函数中调用代码,以确保您的
警报打印
0
,因为它是在
geocoder
完成其工作之前运行的。当
geocoder
完成时,您可以使用回调来获得通知:

$(document).ready(function() {
    var lat = 0;
    var lng = 0;
    var geocoderFinished = function(){
        alert(lat);
    };
    function getLatLng(address) {
        var geocoder = new google.maps.Geocoder();
        geocoder.geocode({'address': address}, function(results, status) {
            if (status == google.maps.GeocoderStatus.OK) {
                lat = results[0].geometry.location.lat(); //how do I access lat 
                lng = results[0].geometry.location.lng() //and lng outside function ormake it global
                geocoderFinished();
            }
        });
    }
    getLatLng();
});

您的
警报
打印
0
,因为它是在
geocoder
完成其工作之前运行的。当
geocoder
完成时,您可以使用回调来获得通知:

$(document).ready(function() {
    var lat = 0;
    var lng = 0;
    var geocoderFinished = function(){
        alert(lat);
    };
    function getLatLng(address) {
        var geocoder = new google.maps.Geocoder();
        geocoder.geocode({'address': address}, function(results, status) {
            if (status == google.maps.GeocoderStatus.OK) {
                lat = results[0].geometry.location.lat(); //how do I access lat 
                lng = results[0].geometry.location.lng() //and lng outside function ormake it global
                geocoderFinished();
            }
        });
    }
    getLatLng();
});

处理异步操作时,请考虑使用回调函数:

function getLatLng(address, callback) {
    var geocoder = new google.maps.Geocoder();
    geocoder.geocode({'address': address}, function (results, status) {
        if (status == google.maps.GeocoderStatus.OK) {
            lat = results[0].geometry.location.lat(); //how do I access lat 
            lng = results[0].geometry.location.lng() //and lng outside function ormake it global
            callback(lat, lng);
        }
    });
}

getLatLng(function(lat, lng) {
    alert([lat, lng]);
});

处理异步操作时,请考虑使用回调函数:

function getLatLng(address, callback) {
    var geocoder = new google.maps.Geocoder();
    geocoder.geocode({'address': address}, function (results, status) {
        if (status == google.maps.GeocoderStatus.OK) {
            lat = results[0].geometry.location.lat(); //how do I access lat 
            lng = results[0].geometry.location.lng() //and lng outside function ormake it global
            callback(lat, lng);
        }
    });
}

getLatLng(function(lat, lng) {
    alert([lat, lng]);
});

警报不在回调函数中,因此它实际上是在地理代码调用完成之前运行的。向上移动1行。您已经可以访问
lat
了。问题在于
geocode
调用异步运行,并且在执行
警报时尚未完成。使用暴力无法解决这一问题:在收到结果之前不可能打印结果。geocoder.geocode()是一个异步过程,因此需要在回调函数中发出警报。警报的可能副本不在回调函数中,因此它实际上是在geocode调用完成之前运行的。向上移动1行。您已经可以访问
lat
了。问题在于
geocode
调用异步运行,并且在执行
警报时尚未完成。使用暴力无法解决这一问题:在收到结果之前不可能打印结果。geocoder.geocode()是一个异步过程,因此需要在回调函数中发出警报。可能重复+1以简要说明异步调用的性质,但-1甚至考虑使用
setTimeout
。调用需要多长时间?
setTimeout
对于此类操作来说是一种不好的模式。+1用于简要说明异步调用的性质,而-1用于考虑使用
setTimeout
。调用需要多长时间?
setTimeout
对于此类操作来说是一种不好的模式。