jQuery使用.slice()。每个都在不同的索引上启动循环

时间:2013-06-11 13:01:54

标签: jquery each slice

我需要读取递增的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>

1 个答案:

答案 0 :(得分:1)

切片数组时,会创建一个新数组。这导致'index'与你的each()函数内的'index'之间的不匹配。例如,当index == 0时,它不是整个DOM中的第一条推文,它是切片数组中的第一条推文。

我认为解决方案是将'length'设置为切片数组的长度,而不是原始数组。有点像:

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);
}