.each循环中的setInterval或setTimeout
setInterval or setTimeout inside an .each loop
这段代码将通过传递从XML文件中获取的参数来调用函数。一旦将参数传递给该函数,该函数将在DIV中写入内容。我遇到的问题是,函数应该每次写一次,每2秒才写一次。
这就是我到目前为止所做的,但不幸的是,函数是在同一时间被调用的,DIVs是在同一时间被编写的。看起来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>
Try
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);
});
});
演示:恰好
相关文章:
- setInteval vs setTimeout
- 如何在chrome扩展中存储数据/结果,以及如何使用setTimeout使其只被调用一次
- setTimeout可以与闭包内的函数一起使用吗
- Javascript setTimeout for an array
- 刷新后,setTimeout将工作或不工作
- 继续使用javascript中的setTimeout
- setTimeout函数能否在其前面的代码执行之前激发
- jQuery setTimeOut: in for-loop
- SetTimeout and clearTimeout in Javascript
- 在setTimeout中调用相同的函数
- setTimeout不能太长
- Javascript-SetTimeout导致浏览器冻结
- setTimeout and V8
- 将jQuery对象传递到setTimeout递归函数中
- 是定义的操作系统睡眠/挂起期间setTimeout的行为
- 添加一个setTimeout方法会使jQuery悬停方法忽略setTimeout方法中的函数
- JS:setTimeout函数的UI计数
- 使setInterval和setTimeout工作时出现问题
- 为什么setTimeout适用于无休止的递归调用
- settimeout调用的函数未结束