.each循环递增索引

时间:2013-06-11 14:15:59

标签: jquery

我正在尝试创建一个递归函数并递增循环的索引,以便它将从最新的索引循环开始读取XML文件。 但由于某些原因,循环总是从XML文件的开头重新开始,而不是从给定的索引开始,在这里传递:processXML(indexValue + 1); 我该如何解决这个问题? 我如何告诉jQuery重新启动函数但是从最后给定的索引循环而不是从开始循环?

请帮助我,这让我发疯了。谁给了我正确的答案我肯定会接受它。 谢谢你的帮助!

function processXML(indexValue) {
    $.ajax({
        url: "../broadcasted.xml",
        dataType: "xml",
        cache: false,
        success: function(d) {
            var length = $(d).find('tweet').length;
            $(d).find('tweet').each(function(indexValue) {

                var cvdIndexId = $(this).find("index");
                var cvdTweetAuthor = $(this).find("author").text();
                var cvdTweetDescription = $(this).find("description").text();
                setTimeout(function() {
                    if (indexValue == (length - 1)) {
                        processXML(indexValue + 1);
                        //return false;
                        //count = 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('');
                    }
                }, indexValue * 1000);
            });
        }

    });
}

1 个答案:

答案 0 :(得分:0)

尝试这样的事情。调用broadcastedXMLProcessStart()将其启动。

var broadcastedXML;
var broadcastedXMLIndex=0;

function broadcastedXMLProcessStart() {
    $.ajax({
        url: "../broadcasted.xml",
        dataType: "xml",
        cache: false,
        success: function(d) {
          broadcastedXML = d;
          // set broacastedXMLIndex=0 here to start from beginning
          broadcastedXMLProcessTick();
        }
    });
}

function broadcastedXMLProcessTick() {
    var d = broadcastedXML;
    var nextIndex = broadcastedXMLIndex;

    var length = $(d).find('tweet').length;
    $(d).find('tweet').eq(nextIndex).each(function(idx) {    
        var cvdIndexId = $(this).find("index");
        var cvdTweetAuthor = $(this).find("author").text();
        var cvdTweetDescription = $(this).find("description").text();

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

    });
    if(nextIndex+1 > length) {
      setTimeout('broadcastedXMLProcessStart()', 1000);
    } else {
      broadcastedXMLIndex++;
      setTimeout('broadcastedXMLProcessTick()', 1000);
    }
}