0

我正在尝试创建一个递归函数并增加循环的索引,以便它将开始从最新的索引循环中读取 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);
            });
        }

    });
}
4

1 回答 1

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);
    }
}
于 2013-06-11T14:32:29.273 回答