将变量从回调函数传递给另一个函数? [英] Passing a variable from a callback function to another function?

查看:102
本文介绍了将变量从回调函数传递给另一个函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在设置HTML5 GeoLocation脚本,我想将邮政编码存储在cookie中,但是现在我只是想弄清楚如何将邮政编码变量传递给另一个函数.

I am working on setting up an HTML5 GeoLocation script and I would like to store the zip code in a cookie but for now I am just trying to figure out how to pass the zip code variable into another function.

这是我的脚本,用于根据纬度/经度反转地理编码:

Here is my script to reverse geo-code based on lat/long:

function retrieve_zip(callback)
{
    try { if(!google) { google = 0; } } catch(err) { google = 0; } // Stupid Exceptions
    if(navigator.geolocation) // FireFox/HTML5 GeoLocation
    {
        navigator.geolocation.getCurrentPosition(function(position)
        {
            zip_from_latlng(position.coords.latitude,position.coords.longitude,callback);
        });
    }
    else if(google && google.gears) // Google Gears GeoLocation
    {
        var geloc = google.gears.factory.create('beta.geolocation');
        geloc.getPermission();
        geloc.getCurrentPosition(function(position)
        {
            zip_from_latlng(position.latitude,position.longitude,callback);
        },function(err){});
    }
}
function zip_from_latlng(latitude,longitude,callback)
{
    // Setup the Script using Geonames.org's WebService
        var script = document.createElement("script");
        script.src = "http://ws.geonames.org/findNearbyPostalCodesJSON?lat=" + latitude + "&lng=" + longitude + "&callback=" + callback;
        console.log(script.src);
    // Run the Script
        document.getElementsByTagName("head")[0].appendChild(script);
}
function callback(json)
{
    zip = json.postalCodes[0].postalCode;
    country = json.postalCodes[0].countryCode;
    state = json.postalCodes[0].adminName1;
    county = json.postalCodes[0].adminName2;
    place = json.postalCodes[0].placeName;
    alert(zip);
}
$('#findLocation').click(function(event) {
    event.preventDefault();
    console.log(zip); // This is giving me undefined currently
});

因此,基本上,在回调函数中,我想将邮政编码存储为变量(而不是在警报中显示),然后在底部的on click函数中,我希望能够显示以前的回调函数中存储的邮政编码.

So basically, in the callback function, I want to store the zip code as a variable(rather than displaying it in an alert) and then in the on click function at the bottom, I want to be able to display the zip code that was stored in the previous callback function.

非常感谢任何帮助,对于Javscript/jQuery还是很新的,谢谢!

Any help greatly appreciated, still pretty new to Javscript/jQuery, thanks!

推荐答案

您可以将zip设置为全局"变量,方法是将其包含在文档顶部的函数之外,如下所示:

You could set zip as a 'global' variable by including it outside of the function at the top of the document like so:

var zip;
...

或者,您可以考虑在全局"级别定义一个对象,并将其用作命名空间来存储变量,如下所示:

Alternatively, you may consider defining an object at the 'global' level and using it as a namespace to store variables like so:

window.address = {};

function callback(json){
    address.zip = json.postalCodes[0].postalCode;
    address.country = json.postalCodes[0].countryCode;
    address.state = json.postalCodes[0].adminName1;
    address.county = json.postalCodes[0].adminName2;
    address.place = json.postalCodes[0].placeName;
}

$('#findLocation').click(function(event) {
    event.preventDefault();

    console.log(address.address);
    console.log(address.zip);
    ...
});

我希望这会有所帮助!

这篇关于将变量从回调函数传递给另一个函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆