简体   繁体   English

如何在包含此功能之外的TimeZoneDB API响应的jQuery函数中获取变量的值或数组?

[英]How get value or array of a variable in a jQuery function which contains the response of the TimeZoneDB API outside of this function?

I'm new in programming and have a little problem. 我是编程新手,有点问题。 I'm programming a map in which you can get the next overflight of a specific satellite by clicking in a map. 我正在编程一张地图,在其中您可以通过单击地图来获得特定卫星的下一次飞越。 But the timestamp in the acquisition plan of this satellite is written in UTC. 但是,这颗卫星的采购计划中的时间戳是用UTC编写的。 Therefore, I'm calculating the time difference/zone of this location with the TimeZoneDB API ( https://timezonedb.com/api ). 因此,我正在使用TimeZoneDB API( https://timezonedb.com/api )计算此位置的时差/区域。 The request is send by clicking in the map and the response is the current time and date of this location in JSON format in the attribute formatted. 通过在地图中单击来发送请求,并且响应是JSON格式(格式设置为属性)中该位置的当前时间和日期。 This timestamp will be substracted from the UTC time. 该时间戳将从UTC时间中减去。 The final time zone is shown in a popup, then. 然后,最后时区显示在弹出窗口中。 But I have the problem, I don't get the variable with the current date global. 但是我有一个问题,我没有全局当前日期的变量。 I've tried to save it in the local storage and recall it, which works fine, but doesn't look good. 我试图将其保存在本地存储中并重新调用它,虽然效果很好,但看起来并不好。 And by using this way another problem faces up. 通过使用这种方式,另一个问题将面临。 Sometimes showing the popup content is faster than the response of the server and shows no value. 有时,显示弹出内容的速度比服务器的响应速度快,并且没有任何价值。 My JavaScript/jQuery code of sending the request and getting the date in a variable looks like this (the API key, longitude and latitude were defined in variables before this jQuery function): 我的发送请求并获取变量中日期的JavaScript / jQuery代码如下所示(API键,经度和纬度是在此jQuery函数之前的变量中定义的):

$.getJSON("http://api.timezonedb.com/v2/get-time-zone?key="+key+"&format=json&by=position&lat="+lat+"&lng="+lon, function(localdata) {
localtime = localdata.formatted;
localStorage.setItem('loct', localtime);
});

var localtime = localStorage.getItem('loct');
localtime = new Date (localtime);
localtime = localtime.getTime();
localStorage.removeItem('loct');

I know there might be similar questions, but I didn't get to solve it by a callback function. 我知道可能存在类似的问题,但是我没有通过回调函数来解决它。 I guess I'm doing something wrong. 我想我做错了。 It will be great if somebody can help me :-) 如果有人可以帮助我,那就太好了:-)

Best regards Anna 最好的问候安娜

Something like this not tested 像这样的东西未经测试

    function getTimeZone(key, lat, lon, callback) {
    $.getJSON("http://api.timezonedb.com/v2/get-time-zone?key=" + key + "&format=json&by=position&lat=" + lat + "&lng=" + lon, function (localdata) {
        localtime = localdata.formatted;
        localStorage.setItem('loct', localtime);
        if (typeof callback == 'function') callback(localtime);
    });
}

var localtime = localStorage.getItem('loct');

if (!localtime) {
    //key,lat,lon you obviously have this set somewhere
    getTimeZone(key, lat, lon, function (localtime) {

        localtime = new Date(localtime);
        localtime = localtime.getTime();
        console.log("localtime", localtime)
        localStorage.removeItem('loct');
    })
}

You could clean that up by checking if localstorage loct key doesn't exist then call the getTimeZone... 您可以通过检查localstorage loct键是否不存在来清理该错误,然后调用getTimeZone ...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM