0

我有这段代码,它将通过传递从 XML 文件中获取的参数来调用函数。将参数传递给函数后,该函数会将内容写入 DIV。我遇到的问题是该函数应该每次写入一次,并且每 2 秒才写入一次。

这是我到目前为止所做的,但不幸的是,函数被同时调用,DIV 被同时编写。看起来 setInterval 不像我预期的那样工作:

每个 XML 数据每 2 秒调用一次的函数是:

obj = $('#cvd_bubble_left').append(makeCvdBubbleAnimator(cvdIndexId, cvdTweetAuthor, cvdTweetDescription));
    obj.fitText(7.4);

整个代码是:

var tocURL = "../broadcasted.xml";
$.get(tocURL, function(d) {
    $(d).find('tweet').each(function() {
        var cvdIndexId = $(this).find("index");
        var cvdTweetAuthor = $(this).find("author").text();
        var cvdTweetDescription = $(this).find("description").text();
        setInterval(function() {
            if (cvdTweetAuthor === "Animator") {
                obj = $('#cvd_bubble_left').append(makeCvdBubbleAnimator(cvdIndexId, cvdTweetAuthor, cvdTweetDescription));
                obj.fitText(7.4);
            } else {
                obj = $('#cvd_bubble_right').append(makeCvdBubble(cvdIndexId, cvdTweetAuthor, cvdTweetDescription));
                obj.fitText(7.4);
            }
        }, 2000);
    });
});

xml代码是:

<?xml version="1.0" encoding="UTF-8"?>
<root bubbles="6">
    <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>
</root>
4

1 回答 1

0

尝试

var tocURL = "../broadcasted.xml";
$.get(tocURL, function(d) {
    $(d).find('tweet').each(function(index) {
        var cvdIndexId = $(this).find("index");
        var cvdTweetAuthor = $(this).find("author").text();
        var cvdTweetDescription = $(this).find("description").text();
        setTimeout(function() {
            if (cvdTweetAuthor === "Animator") {
                obj = $('#cvd_bubble_left').append(makeCvdBubbleAnimator(cvdIndexId, cvdTweetAuthor, cvdTweetDescription));
                obj.fitText(7.4);
            } else {
                obj = $('#cvd_bubble_right').append(makeCvdBubble(cvdIndexId, cvdTweetAuthor, cvdTweetDescription));
                obj.fitText(7.4);
            }
        }, index * 2000);
    });
});

演示:Plunker

于 2013-06-11T08:55:17.280 回答