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

配列をスライスすると、新しい配列が作成されます。これにより、「インデックス」の意味と実際の each() 関数内の内容が一致しなくなります。たとえば、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 に答える