0


I want to use variable current outside of my function, I tried many logic's but i just got undefined variable in console. Is there something that i am missing or doing wrong?

var interval = null;
$(document).ready(function() {
  interval = setInterval(updateDiv, 1000);
});

function updateDiv() {
  var previous = "";
  var postIDAPI = "http://localhost/test/json.php?shortURL=1";
  $.getJSON(postIDAPI, function(json) {
    var current = json.postid;
    console.log('PostID : ', current);
    $("#address").html(current);
    if (previous !== current) {
      clearInterval(interval);
    };
  });
};
var postID = current;
//here i want to use variable current

Thanks, Any help will be appriciated.

Barmar
  • 741,623
  • 53
  • 500
  • 612
Risky Jatt
  • 31
  • 1
  • 5
  • Don't define it in the function? But then you'll run into that whole "A" part of Ajax. – Dave Newton Jan 05 '17 at 19:57
  • Why would you want this? Having a variable outside will clutter the global scope. – Baruch Jan 05 '17 at 19:57
  • You could declare it globally outside the updateDiv() function, but bearing your ajax use why would you want to do that? – Syden Jan 05 '17 at 19:59
  • That doesn't make sense. You want to use `current` before it could even have gotten a value. `var postID = current;` is executed before `updateDiv` was ever called. Also, `updateDiv` is potentially called multiple times which could assign different values to `current`. – Felix Kling Jan 05 '17 at 19:59
  • actually i need that because i want to use that variable in another function.. i tried globally but it always returns undefined_variable – Risky Jatt Jan 05 '17 at 20:01
  • @RiskyJatt I think you need to take a step back and consider how asynchronous programming works. The only time that value is consistently valid is in the success callback itself. – Dave Newton Jan 05 '17 at 20:02
  • *"i want to use that variable in another function"* How/when/where is that function executed? – Felix Kling Jan 05 '17 at 20:03
  • Why exactly are you trying to change the scope of `current`? – kawnah Jan 05 '17 at 20:03
  • actually i am a noob, can someone here help me out with code? :( – Risky Jatt Jan 05 '17 at 20:13

2 Answers2

4

Declare current at a higher scope so that it is available there, and then reference it directly (without a var statement) in your inner function, like so:

var interval = null;
var current;
$(document).ready(function() {
    interval = setInterval(updateDiv, 1000);
});

function updateDiv() {
    var previous = "";
    var postIDAPI = "http://localhost/test/json.php?shortURL=1";
    $.getJSON(postIDAPI, function(json) {
        current = json.postid;
        console.log('PostID : ', current);
        $("#address").html(current);
        if (previous !== current) {
            clearInterval(interval);
        }
    });
}
var postID = current;
//here i want to use variable current

As commenter Dave Newton pointed out below, however, current will not actually be available at this point until the data is returned from your ajax call and your callback has executed-- as such, you may be better wrapping whatever additional functionality you need here in a function that's called at the end of your callback (if this is pertinent to your case).

Alexander Nied
  • 12,804
  • 4
  • 25
  • 45
  • 3
    This won't resolve the underlying issue of asynchronicity. – Dave Newton Jan 05 '17 at 19:58
  • An excellent point-- my answer may as such prove to be insufficient-- I'll make a comment noting as such. – Alexander Nied Jan 05 '17 at 19:59
  • Also, since `updateDiv` will be called multiple times it's just illogical to access `current` once. It's almost paradoxical. – Felix Kling Jan 05 '17 at 20:02
  • @FelixKling is this possible or not? if possible can you please provide some example with my code? – Risky Jatt Jan 06 '17 at 06:54
  • @RiskyJatt - I think FelixKling is stating not that it wouldn't _work_, per se, but that it's perhaps poorly thought out/designed (hopefully he or she doesn't mind my speculating as to their meaning). However, without a clearer understanding of what it is you are trying to achieve, and why you are trying to achieve it in the way you are, it is hard to give you a better sense of whether or not it will work. If you have some other code outside the block that variably runs that you want to have access to "current" then it should be fine. If it needs to have access immediately, probably not... – Alexander Nied Jan 06 '17 at 13:55
0

You can declare the variable outside $(document).ready()

var interval = null, current;

$(document).ready(function(){
    interval = setInterval(updateDiv,1000);
});

function updateDiv(){
    var previous = "";
    var postIDAPI = "http://localhost/test/json.php?shortURL=1";
    $.getJSON(postIDAPI, function (json) {
        current = json.postid;
        console.log('PostID : ', current);
        $("#address").html(current);
        if (previous !== current) {
            clearInterval(interval);
        };
   });
};
var postID = current;
mrid
  • 5,782
  • 5
  • 28
  • 71