开发者

How do I make jQuery JSON returned data available globally & Minor string parse help?

开发者 https://www.devze.com 2023-01-29 09:33 出处:网络
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.

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;
    });
}


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


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
}
0

精彩评论

暂无评论...
验证码 换一张
取 消