如何使 jQuery JSON 返回的数据在全球范围内可用?小字符串解析有帮助吗?

发布于 2024-10-06 22:45:54 字数 1773 浏览 5 评论 0原文

我在 $.getJSON 的帮助下进行了 API 调用,但我不知道如何使返回的数据在全局范围内可用,以便在其他函数中使用。

下面代码中的return不起作用,但如果我alert它:它会起作用。

$.getJSON(url, function(timeInfo){
        return timeInfo.time;
});

也出了2010-12-09 12:53的时间格式。我如何取出 12(小时)。

请帮忙,很抱歉将两个问题合二为一。

详细代码:

function handle_geolocation_query(position) {
    if(position[0] == 'Yes'){
        var url = "http://ws.geonames.org/findNearByWeatherJSON?lat=" + position[1] + "&lng=" + position[2] + "&callback=?";
    } else {
        var url = "http://ws.geonames.org/findNearByWeatherJSON?lat=" + position.coords.latitude + "&lng=" + position.coords.longitude + "&callback=?";
    }
    $.getJSON(url, function(weatherInfo){
        var clouds = weatherInfo.weatherObservation.clouds;
        var weather = weatherInfo.weatherObservation.weatherCondition;
        var time = getTimeInfo(position);
        if(time.getHours()>=6 && time.getHours()<=18){
            var weatherClass = placeImageDay(clouds, weather);
            $('#weatherImage').show().addClass(weatherClass);
        } else {
            var weatherClass = placeImageNight(clouds, weather);
            $('#weatherImage').show().addClass(weatherClass);
        }
    });
}

function getTimeInfo(position){
    if(position[0] == 'Yes'){
        var url = "http://ws.geonames.org/timezoneJSON?lat=" + position[1] + "&lng=" + position[2] + "&callback=?";
    } else {
        var url = "http://ws.geonames.org/timezoneJSON?lat=" + position.coords.latitude + "&lng=" + position.coords.longitude + "&callback=?";
    }
    $.getJSON(url, function(timeInfo){
        return timeInfo.time;
    });
}

I have made an API call with the help of $.getJSON but I don't know how do I make the returned data available globally to be used in other functions as well.

The return in below code doesn't work but instead if I alert it: it works charms.

$.getJSON(url, function(timeInfo){
        return timeInfo.time;
});

Also out of 2010-12-09 12:53 time format. How do I take the 12 (the hour) out.

Please help and sorry for including two questions in one.

Detailed code:

function handle_geolocation_query(position) {
    if(position[0] == 'Yes'){
        var url = "http://ws.geonames.org/findNearByWeatherJSON?lat=" + position[1] + "&lng=" + position[2] + "&callback=?";
    } else {
        var url = "http://ws.geonames.org/findNearByWeatherJSON?lat=" + position.coords.latitude + "&lng=" + position.coords.longitude + "&callback=?";
    }
    $.getJSON(url, function(weatherInfo){
        var clouds = weatherInfo.weatherObservation.clouds;
        var weather = weatherInfo.weatherObservation.weatherCondition;
        var time = getTimeInfo(position);
        if(time.getHours()>=6 && time.getHours()<=18){
            var weatherClass = placeImageDay(clouds, weather);
            $('#weatherImage').show().addClass(weatherClass);
        } else {
            var weatherClass = placeImageNight(clouds, weather);
            $('#weatherImage').show().addClass(weatherClass);
        }
    });
}

function getTimeInfo(position){
    if(position[0] == 'Yes'){
        var url = "http://ws.geonames.org/timezoneJSON?lat=" + position[1] + "&lng=" + position[2] + "&callback=?";
    } else {
        var url = "http://ws.geonames.org/timezoneJSON?lat=" + position.coords.latitude + "&lng=" + position.coords.longitude + "&callback=?";
    }
    $.getJSON(url, function(timeInfo){
        return timeInfo.time;
    });
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

稚气少女 2024-10-13 22:45:54

您可以从 $.getJSON() 中的成功回调函数将数据传递给其他函数和对象。这样做的方法是创建一个全局对象或函数:

var MYWEBSITE = { data: {} };
var myGlobalFunction = function(data) { 
  alert("The time I got is: " + data.time);
};

$.getJSON(url, function(timeInfo) {

  // set the data property on MYWEBSITE
  MYWEBSITE.data = timeInfo;

  // execute the function and pass the parameter
  myGlobalFunction(timeInfo);
});

编辑添加了将全局函数作为第二个参数传递的示例

您还可以将全局函数作为第二个参数传递:

var globalFunction = function(data) { ... };

$.getJSON(url, globalFunction);

这样,globalFunction 就会传递响应中返回的任何内容。

编辑 2添加了时间提取的正则表达式示例

要提取日期的小时部分,您可以使用正则表达式来匹配组件:

var time = "2010-12-09 12:53", hour = "";
var timeMatcher = /^([0-9]{4}-[0-9]{2}-[0-9]{2})\s([0-9]{2}):([0-9]{2})$/;
if(timeMatcher.test(time)) {
  hour = timeMatcher.match(time)[2]; // finds the second brace, hour should contain "12"
}

有关详细信息,请参阅http://en.wikipedia.org/wiki/Regular_expression

You can pass the data to other functions and objects from the success callback function in $.getJSON(). The way you do that is either create a global object or function:

var MYWEBSITE = { data: {} };
var myGlobalFunction = function(data) { 
  alert("The time I got is: " + data.time);
};

$.getJSON(url, function(timeInfo) {

  // set the data property on MYWEBSITE
  MYWEBSITE.data = timeInfo;

  // execute the function and pass the parameter
  myGlobalFunction(timeInfo);
});

EDIT: Added example of passing global functions as second parameter

You can also pass a global function as second parameter:

var globalFunction = function(data) { ... };

$.getJSON(url, globalFunction);

That way, globalFunction gets passed whatever is returned in the response.

EDIT 2: Added regular expression example for time extraction

To extract the hour portion of the date, you can use a Regex to match the components:

var time = "2010-12-09 12:53", hour = "";
var timeMatcher = /^([0-9]{4}-[0-9]{2}-[0-9]{2})\s([0-9]{2}):([0-9]{2})$/;
if(timeMatcher.test(time)) {
  hour = timeMatcher.match(time)[2]; // finds the second brace, hour should contain "12"
}

For more info, see http://en.wikipedia.org/wiki/Regular_expression

清眉祭 2024-10-13 22:45:54

这是异步调用,您不能返回类似的值,它只是实例化进程并继续,您希望在返回处理程序中执行的任何操作

   $.getJSON(url, function(timeInfo){
            processData(timeInfo)
    });

function processData(Info)
{
///do acess them here
}

This is asyncronous call and you cannot return a value like , it just instantiate the proces sand goes on , anything you want you have do inside return handler

   $.getJSON(url, function(timeInfo){
            processData(timeInfo)
    });

function processData(Info)
{
///do acess them here
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文