-1

I still struggle with functions, variables and scope.

I've read several articles "explaining" the concepts, plus a number of seemingly related posts but cannot figure this one out. How can I access the vars ip/prov/country outside the function?

Here is working code (vars accessed inside the fn):

$.get("http://ipinfo.io", function (response) {
    var ip, prov, country;
    ip = response.ip;
    prov = response.region;
    country = response.country;
    alert( 'IP: '+ip+ ' ** Prov: ' +prov+ ' ** Country: ' +country );
}, "jsonp");

However, I want to access the vars outside the function, to use later on.

This did not work on my site (all items displayed as "undefined"):

var ip, prov, country;
$.get("http://ipinfo.io", function (response) {
    ip = response.ip;
    prov = response.region;
    country = response.country;
}, "jsonp");
alert( 'IP: '+ip+ ' ** Prov: ' +prov+ ' ** Country: ' +country );

However, it worked just fine in this jsFiddle !

Also, this did not work (all undefined):

var ip, prov, country;
$(function(){
    $.get("http://ipinfo.io", function (response) {
        ip = response.ip;
        prov = response.region;
        country = response.country;
    }, "jsonp");
    alert( 'IP: '+ip+ ' ** Prov: ' +prov+ ' ** Country: ' +country );
}); //END document.ready

What must I do on my site to access the ip/prov/country vars outside the $.get function?

halfer
  • 19,824
  • 17
  • 99
  • 186
crashwap
  • 2,846
  • 3
  • 28
  • 62
  • this is the most duplicated question on SO, i guess... Make some effort, use the search field – A. Wolff Feb 17 '14 at 17:46
  • If it's that duplicated, why not just help? I've been struggling with this for hours. Granted, I'm not the sharpest knife in the drawer, but I have figured out many other problems. My honest need for a helping hand is the reason I've asked the question. – crashwap Feb 17 '14 at 17:54
  • it only works in your fiddle because you don't happen to click the button until the ajax is done... – dandavis Feb 17 '14 at 18:34
  • Then read the link, your question is a duplicate of that one, and there are several *excellent* answers there. – user229044 Feb 17 '14 at 18:37

1 Answers1

5

AJAX is asynchronous. Your alert needs to go inside the callback function. This is not a matter of variable scoping, the variables simply aren't being set when your alert executes, because the AJAX call hasn't completed.

user229044
  • 232,980
  • 40
  • 330
  • 338
  • Thanks for the answer, but this isn't the problem I'm having. There is no AJAX code in my source yet (although, down the road, I intend to send these vars via AJAX to the server for storage.) For now, I only want to use them without having to move the AJAX code inside the `$.get` function. – crashwap Feb 17 '14 at 18:25
  • 2
    what i'm hearing: "For now, i only want to do something impossible." $.get IS ajax, so you are using ajax whether you know it or not. You can push the info into a global to be reached from outside the callback, but scope is not your main problem here, timing is. And if you have your outside code waiting for that global to be available, why not just handle it in the callback instead of poll until something is no longer undefined? – dandavis Feb 17 '14 at 18:31
  • Yes, this *is* the problem you're having. Your code is *nothing but* AJAX. That's what `$.get` is. – user229044 Feb 17 '14 at 18:37
  • 1
    Oh! You guys are totally right! How does one apologize for being an absolute arse and criticising the fellow who not only tries to help, but actually successfully identifies the true problem? As Clarence Darrow said, "Ever since the Phoenicians invented upvoting, there's only been one answer to that question." Most humble apolgies, and sincere thanks. – crashwap Feb 17 '14 at 19:32
  • @dandavis Thanks for that crucial reminder. I've been n2k (nose-to-keyboard) for too long and just didn't see that. Reviewed several of your other answers -- many excellent responses. Thanks. +1 – crashwap Feb 17 '14 at 20:46
  • @crashwap: np, just glad to be useful... – dandavis Feb 18 '14 at 23:49