0

Here is what i'm trying to do:

$.getJSON( "http://api.openweathermap.org/data/2.5/weather?lat="+latitude+"&lon="+longitude", function(data) { 
fh = data;
});

I'm trying to do a

console.log(fh);

outside $.getJSON

Is it possible to get the fh var outside this function ? I'm trying this and shows me an error at the console:

Uncaught ReferenceError: fh is not defined

  • Why are you trying to access the data outside the function in the first place? What does "outside this function" even mean? It probably won't work as you expect. Please read [How do I return the response from an asynchronous call?](http://stackoverflow.com/q/14220321/218196) and [Why is my variable unaltered after I modify it inside of a function? - Asynchronous code reference](http://stackoverflow.com/q/23667086/218196), two of the top 10 most frequently asked JavaScript questions on Stack Overflow. – Felix Kling Jan 10 '17 at 22:59
  • I'm just trying to learn something man... take it easy. – Rafael Herculano Jan 10 '17 at 23:20
  • And I am just trying to help. If you explain why and where you need to access the data "outside the function", we can provide you a proper solution. You should never have to use a global variable to share the data like this. Usually you pass the response to another function, e.g. `function outside(fh) { console.log(fh); } $.getJSON(..., function(data) { outside(data); });`. – Felix Kling Jan 10 '17 at 23:22
  • Possible duplicate of [How do I return the response from an asynchronous call?](http://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – user2864740 Jan 11 '17 at 00:11

4 Answers4

1

You should create fh as a global variable which will then allow it be updated within the getJSON method and accessed outside of the method.

It will be equal to undefined until a response has been received from the getJSON request to the server due.

For this reason, and to see it working you should use setTimeout. In practice you should use promises, but that may be outside the scope of the question. As long as the server has responded within 2 seconds the value of fh will be set to the data returned from the server.

The code below demonstrates this.

var fh;

$.getJSON("http://api.openweathermap.org/data/2.5/weatherlat=" + latitude + "&lon=" + longitude, function(data) { 
    fh = data;
});

setTimeout(function(){
  console.log(fh)
}, 2000);
Paul Fitzgerald
  • 11,770
  • 4
  • 42
  • 54
0

this non blocking code so that you must wait call back execution

$.getJSON( "http://api.openweathermap.org/data/2.5/weather?lat="+latitude+"&lon="+longitude", function(data) { 

fh = data;
console.log(fh);

});
Isaac
  • 1,442
  • 17
  • 26
Ahmed Kesha
  • 810
  • 5
  • 11
  • While it is certainly possible to do that, `console.log(fh)` will log `null`. Please mention that and explain what the OP should do instead. – Felix Kling Jan 10 '17 at 23:01
0

if you need to use fh outside for something in your logic

you can work around with setInterval function

var fh=null;
$.getJSON("http://api.openweathermap.org/data/2.5/weatherlat="+latitude+"&lon="+longitude", function(data) {
    fh = data;
});

var interval=setInterval(function(){
        if(fh!=null){
           console.log(fh)
           clearInterval(interval)
        }
},500)
Ahmed Kesha
  • 810
  • 5
  • 11
  • While this works, its unnecessarily complex. It would make a lot more sense to pass `data` to a function after it was received. – Felix Kling Jan 10 '17 at 23:24
0

In Side function we need to set the value

localStorage.setItem('fhname',fh);

Outside function we need to get the value

localStorage.getItem('fhname');
Dharman
  • 30,962
  • 25
  • 85
  • 135