Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaScript method begins w/ variables assigned?? very confused

this is my first post, but i'm excited to join this community. I have a question regarding JavaScript which I am completely stumped about.

I'm writing a JavaScript application which pulls data from a server using ajax and adds it to a chart. I'm using Jquery and Highcharts as the framework and then writing my own JavaScript 'wrapper' around Highcharts to produce the interface.

When the processData function get called back with the jSON response, it begins with i=1, even though i shouldn't even be initialized or even declared yet. Other variables are set as well. (I know this from using chrome developer tools to debug). This makes my loop not execute and none of my data gets added to the chart.

I don't know how much code to show, but these are the most relevant parts. I can add more if needed.

function getData(series, min, max, numpts) {
        if (series === undefined) {
            console.log("error on getData");
            return;
        }

        var request = {};
        request.series = series;

        if (min !== undefined) {
            request.start = min;
        } //in seconds
        if (max !== undefined) {
            request.end = max;
        } 
        if (numpts !== undefined) {
            request.numpts = numpts;
        }
        $.getJSON('/data', request, processData);
        return;
    }

    function processData(data) {
        // handle the data after it comes back from an ajax request
        var curSeries,
            chartSeries,
            curPoint;

        for (var i = 0; i < data.length; i ++) {
            curSeries = data[i];
            chartSeries = chart.get(curSeries.name);

            if (chartSeries === null) {
                //alert("oops");
                chart.addSeries(curSeries);
            } else {
                for (var j = 0; j < curSeries.data.length; j ++) {
                    curPoint = curSeries.data[j];
                    chartSeries.addPoint(curPoint, false);
                }
            }
        }
        chart.redraw();
    }

These are both methods of a class I declared called graph.

Thanks if anyone has any ideas! -Matt P

like image 756
MattyP Avatar asked Nov 05 '22 10:11

MattyP


1 Answers

I'd console inspect your data object to make sure it's what you expect, as that loop should be working fine even if i is pre-declared: you're assigning 0 to it at the beginning of the loop, anyway.

The only reason I can think of that i would be defined and initialized before you defined and initialized it is if somewhere else in your codebase you don't initialize the i with the var keyword. Doing that would dump it into the global scope (the window object), making it available via closure to any and every function in your codebase.

If it's not in one of your files, it may be in the highcharts graphing library (in which case run very quickly away from said library).

like image 162
warfangle Avatar answered Nov 11 '22 04:11

warfangle