0

我需要阅读一个递增的 XML 文件。因此,使用递归函数我将最新的值传递index.slice().each但由于某些原因再次调用该函数时它完全停止。怎么了?为什么它不切片到下一个指定的索引?我的代码有什么问题?

function processXML(indexValue) {
    var tocURL = "../broadcasted.xml";
    $.get(tocURL, function(d) {
        var length = $(d).find('tweet').length;
        var count = indexValue;
        $(d).find('tweet').slice(count).each(function(index) {

            var cvdIndexId = $(this).find("index");
            var cvdTweetAuthor = $(this).find("author").text();
            var cvdTweetDescription = $(this).find("description").text();
            setTimeout(function() {
                if (index == (length - 1)) {
                    processXML(index + 1);
                    //alert(index+1);
                } else if (cvdTweetAuthor === "Animator") {
                    $('#cvd_bubble_left').html('');
                    obj = $('#cvd_bubble_left').append(makeCvdBubbleAnimator(cvdIndexId, cvdTweetAuthor, cvdTweetDescription));
                    obj.fitText(7.4);
                    $('#cvd_bubble_right').html('');
                } else {
                    $('#cvd_bubble_right').html('');
                    obj = $('#cvd_bubble_right').append(makeCvdBubble(cvdIndexId, cvdTweetAuthor, cvdTweetDescription));
                    obj.fitText(7.4);
                    $('#cvd_bubble_left').html('');
                }
            }, index * 1000);
        });
    });
}


<?xml version="1.0" encoding="UTF-8"?>
<root bubbles="7">
    <tweet broadcasted="bubble">
        <author><![CDATA[@Liciiious]]></author>
        <description><![CDATA[#EveryoneLovesBeinsport (cc @beinsport @charlesbietry). #pureLIVE]]></description>
        <index>1</index>
    </tweet>
    <tweet broadcasted="bubble">
        <description><![CDATA[Message]]></description>
        <author><![CDATA[beIN Sport]]></author>
        <index>2</index>
    </tweet>
        <tweet broadcasted="bubble">
        <author><![CDATA[@Liciiious2]]></author>
        <description><![CDATA[#EveryoneLovesBeinsport (cc @beinsport @charlesbietry). #pureLIVE]]></description>
        <index>3</index>
    </tweet>
    <tweet broadcasted="bubble">
        <description><![CDATA[Message]]></description>
        <author><![CDATA[Animator]]></author>
        <index>4</index>
    </tweet>
        <tweet broadcasted="bubble">
        <author><![CDATA[@MAuricious]]></author>
        <description><![CDATA[#EveryoneLovesBeinsport (cc @beinsport @charlesbietry). #pureLIVE]]></description>
        <index>5</index>
    </tweet>
    <tweet broadcasted="bubble">
        <description><![CDATA[Message]]></description>
        <author><![CDATA[beIN Sport]]></author>
        <index>6</index>
    </tweet>
        <tweet broadcasted="bubble">
        <description><![CDATA[Messagexxx]]></description>
        <author><![CDATA[beIN Sportxxx]]></author>
        <index>7</index>
    </tweet>
        <tweet broadcasted="bubble">
        <description><![CDATA[Messagexxxzzzz]]></description>
        <author><![CDATA[beIN Sportxxxzzzz]]></author>
        <index>8</index>
    </tweet>
</root>
4

1 回答 1

1

对数组进行切片时,会创建一个新数组。这导致“索引”的含义与每个()函数内部的实际内容不匹配。例如,当 index == 0 时,它不是整个 DOM 中的第一条推文,而是切片数组中的第一条推文。

我认为解决方案是将“长度”设置为切片数组的长度,而不是原始数组。类似于以下内容:

var count = indexValue;    
var new_tweets = $(d).find('tweet').slice(count);
var length = new_tweets.length;

new_tweets.each(function(index) {

您还需要在超时时间内更改传递给 processXML 的参数。

if (index == (length - 1)) {
    processXML(count + length);
    //alert(index+1);
}    
于 2013-06-11T13:12:06.173 回答