簡體   English   中英

jQuery從另一個函數獲取變量

[英]jquery getting variable from another function

我有一個函數來獲取當前地址(運行良好),該地址由另一個jquery函數觸發,我使用該函數將ajax的結果傳遞給mysql。

腳本:

var currentlocation;

function getlocation(){

        navigator.geolocation.getCurrentPosition(
            function( position ){ 

                var lat = position.coords.latitude;
                var lng = position.coords.longitude;
                var google_map_pos = new google.maps.LatLng( lat, lng );

                var google_maps_geocoder = new google.maps.Geocoder();
                google_maps_geocoder.geocode(
                    { 'latLng': google_map_pos },
                    function( results, status ) {
                        if ( status == google.maps.GeocoderStatus.OK && results[0] ) {
                            console.log( results[0].formatted_address );
                            currentlocation = results[0].formatted_address;
                            console.log('cl1: ' + currentlocation);
                        }
                    }
                );
            },
            function(){ 
            }
        );
}

//and jquery function (there are multiple functions similar to this one    using the getlocation()

$(document).ready(function () {
    $('[id^=start-]').on('click', function (e) {
        getlocation();
        console.log('cl2: ' + currentlocation);
        var locationsql = currentlocation;
        console.log('cl3:' + locationsql);
    });
});

控制台會打印出cl2和cl3的未定義結果,然后過一會兒正確顯示google在cl1中的位置結果。

所以我有一個問題是獲取變量currentlocation,以便以后可以在這種情況下使用locationsql

忠告高漲

好了,用jQuery Deferred Method解決了。

我的代碼:

var currentlocationTemp;

function getlocation(){

        // set dfd variable
        var dfd = $.Deferred();

        /* Chrome need SSL! */
        var is_chrome = /chrom(e|ium)/.test( navigator.userAgent.toLowerCase() );
        var is_ssl    = 'https:' == document.location.protocol;
        if( is_chrome && ! is_ssl ){
            return false;
        }
        navigator.geolocation.getCurrentPosition(
            function( position ){ // success cb
                /* Current Coordinate */
                var lat = position.coords.latitude;
                var lng = position.coords.longitude;
                var google_map_pos = new google.maps.LatLng( lat, lng );

                /* Use Geocoder to get address */
                var google_maps_geocoder = new google.maps.Geocoder();
                google_maps_geocoder.geocode(
                    { 'latLng': google_map_pos },
                    function( results, status ) {
                        if ( status == google.maps.GeocoderStatus.OK && results[0] ) {
                            console.log( results[0].formatted_address );
                            currentlocationTemp = results[0].formatted_address;
                            // resolve dfd variable                             
                            dfd.resolve();
                        }
                    }
                );

            },
            function(){ // fail cb
            }

        );
        // promise dfd variable
        return dfd.promise();
   }


$(document).ready(function () {
$('[id^=start-]').on('click', function (e) {

    function start() {
        currentlocation = currentlocationTemp;
        console.log('cl: ' + currentlocation); 
        }

    //// Que of the functions
    getlocation().then(start);  

});
});

由於geocode()函數本身是異步的,因此您必須添加一個回調函數,

function( results, status ) {
                                if ( status == google.maps.GeocoderStatus.OK && results[0] ) {
                                    var currentlocationTemp = results[0].formatted_address;
                                    callback(currentlocationTemp);
                                }
}

並在回調函數中接收您的變量以進行進一步操作

function callback(location){
      currentlocation = location;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM