我有这段代码通过传递从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>
答案 0 :(得分: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