0

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

2 Answers 2

3

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

Sign up to request clarification or add additional context in comments.

2 Comments

I understand that callback is the way. But if you see my detailed code I have added above. I am using one function in another. Can you try to explain how do I manage to do your technique in my code. You don't need to write the code if you don't wish to, just try to throw the concept towards me. Thanks a lot
Like I said, replace return timeInfo.time; in your callback with the appropriate function call to pass timeInfo to the correct handler, exactly as you're already doing in the first callback function in your detailed code (calling placeImageDay and placeImageNight). You can also just put the name of the function into $.getJSON(). I've added the code as an edit to my answer.
0

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
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.