对于Javascript中未定义的循环变量

时间:2016-09-06 05:12:08

标签: javascript html for-loop undefined var

我正致力于实现一个系统,在该系统中可以拖放元素以创建流程图。我的问题是保存流程图,以便在需要时重新加载。现在我已经创建了一个方法,该方法将元素的所有先前数据保存到最终数组中,该数组仅包含放在容器上的元素。但我在调试界面上收到 未定义变量 琐事错误。因此,我没有得到预期的输出,并且在满足条件时不会打印我所包含的警报消息。

上下文中的代码

function saveFlowchart()
{
    var nodes = [];
    var matches = [];
    var searchEles = document.getElementById("container").children;
    for(var i = 0; i < searchEles.length; i++) 
    {
        matches.push(searchEles[i]);
        var idOfEl = searchEles[i].id;

        if(searchEles[i].id !=null || searchEles[i].id !="")
        {
            var $element = $("#" + searchEles[i].id);
            var dropElem = $("#" + searchEles[i].id).attr('class');

            var position = $element.position();
            position.bottom = position.top + $element.height();
            position.right = position.left + $element.width();
            alert("class:"+dropElem+"\nTop position: " + position.top + "\nLeft position: " + position.left + "\nBottom position: " + position.bottom + "\nRight position: " + position.right);

            finalArray[idOfEl-1][0]= idOfEl;
            finalArray[idOfEl-1][1]= dropElem;

            var elId = parseInt(idOfEl);

            if (dropElem == "stream ui-draggable")
            {
                for(var count=0;count<100;count++)
                {
                    alert("One loop opened with count="+count);
                    if(createdImportStreamArray[count][0]==elId)
                    {
                        finalArray[elId-1][2]=  createdImportStreamArray[count][1]; //Selected Stream from Predefined Streams
                        finalArray[elId-1][3]= createdImportStreamArray[count][2]; //asName
                        alert("createdImportStreamArray[count][0]==elId");
                    }
                    else if(createdExportStreamArray[count][0]==elId)
                    {
                        finalArray[elId-1][2]= createdExportStreamArray[count][1]; //Selected Stream from Predefined Streams
                        finalArray[elId-1][3]= createdExportStreamArray[count][2]; //asName
                    }
                    else if(createdDefinedStreamArray[count][0]==elId)
                    {
                        finalArray[elId-1][2]= createdDefinedStreamArray[count][1]; //Stream Name
                        finalArray[elId-1][3]= createdDefinedStreamArray[count][4]; //Number of Attributes
                        finalArray[elId-1][4]=[];
                        for(var f=0;f<createdDefinedStreamArray[r][4];f++)
                        {
                            finalArray[elId-1][4][f][0]=createdDefinedStreamArray[count][2][f][0]; //Attribute Name
                            finalArray[elId-1][4][f][1]=createdDefinedStreamArray[count][2][f][1]; // Attribute Type
                        }
                    }
                    alert("One loop closed with count="+count);
                }
                alert("Loop ended with count="+count);
            }

            else if (dropElem == "wstream ui-draggable")
            {
                ElementType="wstream";
            }

            // else if conditions...

            alert(finalArray);
        }
    }

    //Code to output the connection details in a json format
    //The following is not affected by the above mentioned error

    $(".node").each(function (idx, elem) {
        var $elem = $(elem);
        var endpoints = jsPlumb.getEndpoints($elem.attr('id'));
        console.log('endpoints of '+$elem.attr('id'));
        console.log(endpoints);
        nodes.push({
            blockId: $elem.attr('id'),
            nodetype: $elem.attr('data-nodetype'),
            positionX: parseInt($elem.css("left"), 10),
            positionY: parseInt($elem.css("top"), 10)
        });
    });
    var connections = [];
    $.each(jsPlumb.getConnections(), function (idx, connection) {
        connections.push({
            connectionId: connection.id,
            pageSourceId: connection.sourceId,
            pageTargetId: connection.targetId
        });
    });

    var flowChart = {};
    flowChart.nodes = nodes;
    flowChart.connections = connections;
    flowChart.numberOfElements = numberOfElements;

    var flowChartJson = JSON.stringify(flowChart);
    //console.log(flowChartJson);

    $('#jsonOutput').val(flowChartJson);
}

调试接口

Debugging Interface

根据这个,for循环中的count变量是未定义的。我尝试在循环声明部分之外取第一个语句var count=0并在方法的最开始定义它。但是,这只是根据条件检查count = 0,并且根本不会增加。

在这方面的任何帮助都将受到高度赞赏,因为我已经为这个小错误工作了近2天。

0 个答案:

没有答案