0

thanks in advance for any help and for reading this.

So, I am trying to utilize server data from a $.post response to manipulate graphs. However, this is causing me two problems.

1 - I have tried several times to pass a variable array created from within a $.post {}, which utilizes data returned from the server, into a global scope. However, I must be missing something and tried endless google and search but nothing is working. Can someone hint at the problem? Aka: I would like to have list_1 and list_2 accessible as a global variable outside of the $.post so that I can use it to insert into a tooltip. If I have the array with the necessary information then I can populate the tooltip with such data. Here is a cut down version of the code.

function OnSelectDay(selected) {
    var list_1 = [];
    var list_2 = [];

    var Day = selected.split("/");
    // Format is DD/MM/YYYY
    // Day = Day[0]
    var TheDay = parseInt(Day[0]);
    var TheNextDay = parseInt(Day[0]) + 1;
    $.post("_viewdata.php", { selected: selected }, function (data) {

        for (var i in data.id) {
            var time = data.hora[i];
            time = time.split(":");
            time = parseInt(time);
            var temp = data.dia[i].split("-");
            if (temp[2] == TheDay && time > 20) {
                var num = data.codigo[i].split(".")
                var num2 = parseInt(num[1]);
                if (num2 === 1) { list_1.push(data.hora[i]) };
                if (num2 === 2) { list_2.push(data.hora[i]) };
            }
        };
    }, "json");
    alert(list_1);
}

Problem Number 2:

I am actually creating about 20 arrays and to each I am pushing the value when a logical argument is made. Here is how I am doing it: (also seen above):

if (num2 === 1){list_1.push(data.hora[i])}; if (num2 === 2){list_2.push(data.hora[i])};

When I tried to create this dynamically by using:

if(num2 = num[1]) { var TheList = "list_"+num[1]; TheList.push(data.hora[i]) }

I get an error stating that TheList doesn't have a function. Any ideas why I can't push into this dynamically generated name for the appropriate list? If I replace the "TheList" with "list_1" then it works fine. I still have the problem, though of being within the $.post. It will alert the data only within it and blank outside the $.post{}.

Thanks all,

Howard Renollet
  • 4,609
  • 1
  • 24
  • 31
gamh00
  • 19
  • 6
  • The problem is not that you can't put the value into the global scope, it is that you are trying to read it before you do. asynchronous javascript and xml is asynchronous. – Quentin Dec 17 '14 at 16:51
  • The duplicate will answer your first question. I suggest you create another question for your second problem - in general you can't create and access variables like this, though there are solutions. – James Thorpe Dec 17 '14 at 16:51
  • Thank you Quentin. I had not run into that post. Will look at it now.ny ideas on the dynamically generated list name that gives me an error. – gamh00 Dec 17 '14 at 16:52
  • Because `TheList` is just a string variable holding "list_1", not a reference to the `list_1` array – James Thorpe Dec 17 '14 at 16:53
  • [Check this question/answer](http://stackoverflow.com/questions/1073188/dynamically-referencing-javascript-array-name-without-using-eval) – James Thorpe Dec 17 '14 at 16:54
  • Thanks for the help James. I am pretty new at Javascript. Could you help with how to properly reference/create a dynamic reference to a list array rather than a string variable? – gamh00 Dec 17 '14 at 17:15
  • Quentin, I went over the link that you suggested and I am sorry if I am misunderstanding. If I may ask, aren't I providing the asynchrony by having that function being called on success? Could you please - and thanks in advance - provide with a short example? Do I need to have a separate function that calls upon the function that has the $.post and then return the value to the original function? If so, am I correct to take/understand that the function within the $.post its only to manipulate the response prior to passing to the function calling upon the function with the $.post? – gamh00 Dec 17 '14 at 17:21
  • James, I tried the link that you suggested but I still get the 'undefined' error. I tried var num = data.codigo[i].split(".") followed by window["lista_"+num[1]].push(data.codigo[i]); – gamh00 Dec 17 '14 at 18:18

0 Answers0