找出将setInterval()放在XmlHttpRequest脚本中的位置

时间:2018-09-04 14:45:14

标签: javascript xml xml-parsing xmlhttprequest setinterval

我有一个脚本,可以使用XmlHttpRequest将XML文件中的数据解析为两个单独的表。

我需要调用一个告诉网页每30秒(30000毫秒)重新加载数据的函数。但是,在我尝试放置setInterval()函数的任何地方,都只会导致代码中断。

在这里有任何功能性的地方可以设置它吗?或者是否有其他功能更适合此类脚本?

<head>
<script>

    window.addEventListener("load", function() {
        getRows();
    });

    function getRows() {
        var xmlhttp = new XMLHttpRequest();
        xmlhttp.open("get", "test.xml", true);
        xmlhttp.onreadystatechange = function() {
            if (this.readyState == 4 && this.status == 200) {
                showResult(this);
            }
        };
        xmlhttp.send(null);
    }

    function showResult(xmlhttp) {
        var xmlDoc = xmlhttp.responseXML.documentElement;
        removeWhitespace(xmlDoc);
        var outputResult = document.getElementById("BodyRows");
        var rowData = xmlDoc.getElementsByTagName("Arena");
        var outputResult2 = document.getElementById("BodyRow");
        var rowData2 = xmlDoc.getElementsByTagName("Flag");

        addTableRowsFromXmlDoc(rowData,outputResult);
        addTableFlagsFromXmlDoc(rowData2, outputResult2);
    }

    function addTableRowsFromXmlDoc(xmlNodes,tableNode) {
        var theTable = tableNode.parentNode;
        var newRow, newCell, i;
        console.log ("Number of nodes: " + xmlNodes.length);
        for (i=0; i<xmlNodes.length; i++) {
            newRow = tableNode.insertRow(i);
            newRow.className = (i%2) ? "OddRow" : "EvenRow";
            for (j=0; j<xmlNodes[i].childNodes.length; j++) {
                newCell = newRow.insertCell(newRow.cells.length);
                if (xmlNodes[i].childNodes[j].firstChild) {
                    newCell.innerHTML = xmlNodes[i].childNodes[j].firstChild.nodeValue;
                } else {
                    newCell.innerHTML = "-";
                }
                console.log("cell: " + newCell);
            }
            }
            theTable.appendChild(tableNode);
    }

    function addTableFlagsFromXmlDoc(xmlNodes,tableNode) {
        var theTable = tableNode.parentNode;
        var newRow, newCell, i;
        console.log ("Number of nodes: " + xmlNodes.length);
        for (i=0; i<xmlNodes.length; i++) {
            newRow = tableNode.insertRow(i);
            newRow.className = (i%2) ? "OddRow" : "EvenRow";
            for (j=0; j<xmlNodes[i].childNodes.length; j++) {
                newCell = newRow.insertCell(newRow.cells.length);
                if (xmlNodes[i].childNodes[j].firstChild) {
                    newCell.innerHTML = xmlNodes[i].childNodes[j].firstChild.nodeValue;
                } else {
                    newCell.innerHTML = "-";
                }
                console.log("cell: " + newCell);
            }
            }
            theTable.appendChild(tableNode);
    }

    function removeWhitespace(xml) {
        var loopIndex;
        for (loopIndex = 0; loopIndex < xml.childNodes.length; loopIndex++)
        {
            var currentNode = xml.childNodes[loopIndex];
            if (currentNode.nodeType == 1)
            {
                removeWhitespace(currentNode);
            }
            if (!(/\S/.test(currentNode.nodeValue)) && (currentNode.nodeType == 3))
            {
                xml.removeChild(xml.childNodes[loopIndex--]);
            }
        }
    }

</script>
</head>
<body>
<table id="MainTable">
<tbody id="BodyRows"></tbody>
</table>
<table id="SecondTable">
<tbody id="BodyRow"></tbody>
</table>
</body>

0 个答案:

没有答案