views:

93

answers:

1

Hi everyone, 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

+1  A: 

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).

warfangle
It's also possible that the jQuery library leaks the variable to the global scope, although generally it's pretty well scoped. Anyhow, as you said it shouldn't matter. Javascript has lived with plenty of global variables for decades, it's only lately that knowledge of scoping has been widely spread.
Guffa
So i have been using chromes debugger, and that how I knew that it was already initialized... However, I ran my code through jslint and cleaned up some stuff, mostly spacing and stuff and missing ; and all of a sudden it has stopped doing that... I'm not sure why because nothing really changed near that function. I also think it may have been a chrome issue. I had been operating in the same tab for a while before that, i may have just corrupted the console somehow. :(
Matty P