Does the JavaScript method start with the assigned variables? very embarrassed

This is my first post, but I am happy to join this community. I have a question regarding JavaScript with which I am completely at a dead end.

I am writing a JavaScript application that retrieves data from a server using ajax and adds it to a chart. I use Jquery and Highcharts as a framework, and then create my own JavaScript wrapper around Highcharts to create an interface.

When the processData function receives a callback with a jSON response, it starts with i = 1, although I do not even have to be initialized or even declared. Other variables are also set. (I know this from using chrome debugging tools). This causes my loop to fail, and not one of my data is added to the chart.

I don’t know how much code to display, but these are the most important parts. If necessary, I can add more.

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 class I declared as graph.

Thanks if anyone has any ideas! -Matt P

+5
source share
1 answer

I would index your data object to see what you expect, as this loop should work just fine even if I was previously declared: you assign it 0 at the beginning of the loop.

, , , , - var. ( ), .

, ( ).

+2

All Articles