我怎样才能让这段代码连续地循环遍历项目
How can I get this code to continuously loop through the items?
我有以下代码:
common_load_help("photo.xml");
function common_load_help(file)
{
$(document).ready(function()
{
$.ajax(
{
type: "GET",
url: SITE_URL + "/assets/help/" + file, //call this url
dataType: 'xml',
success: function(xml) //when we have the data...
{
var length = xml.getElementsByTagName("item").length;
console.log("length: " + length);
$('item', xml).each(function(i, el) //go through each help item
{
function looper()
{
$("#design-tips-content").html($(el, this).text());
}
setTimeout(looper, 5000);
});
}
});
});
}
我想要发生的是它把第一个元素放在design-tips-contentdiv中,然后等待5000秒,然后放第二个,然后放第三个,然后循环回到第一个元素。我该怎么做呢?现在看起来它只是显示最后一个元素。
注意:我试图为此创建一个jsfiddle (http://jsfiddle.net/allisonc/c8RLZ/),但我得到错误:MLHttpRequest cannot load http://www.asa.tframes.org:1881/assets/help/photo.xml. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://fiddle.jshell.net' is therefore not allowed access.
success函数中的代码应该可以工作。它的工作原理是构建一个条目文本数组,并将当前索引值存储在一个变量中。然后,它使用setInterval
连续循环遍历所有项。
var tips = $('item', xml).get().map(function(item){
return $(item).text();
});
var currentIndex = 0;
function looper() {
if(currentIndex>=tips.length) {
currentIndex = 0;
}
$('#design-tips-content').html(tips[currentIndex]);
currentIndex++;
}
looper();
setInterval(looper, 5000);
工作小提琴相关文章:
- 从桌面读取python文件时高亮显示代码
- 如何将函数包装在函数中以避免代码重复
- 通过单击表单中的按钮,在代码生成中使用javascript生成字母数字代码
- 货币代码为欧元-金额的格式不应包含小数
- Regex代码只允许一个空格
- 如何将屏幕分辨率乘以 80%,然后在代码中使用
- 从var向代码隐藏函数传递值
- 如何动态插入jquery代码
- 如何在php变量中嵌入JQuery代码
- 代码不会验证
- 如何从rails中的代码中删除新行( )
- Angularjs代码未在匿名函数中运行
- 如何做到这一点,使代码在不传递条件后执行函数
- 如何在javascript中连续执行代码
- 通过http请求将java代码中的连续文本数据发送到html
- 如何在scala代码中用单个空格替换所有连续的下划线
- 连续扫描的代码体
- 我怎样才能让这段代码连续地循环遍历项目
- 对于连续的多个滚动,我希望我的页面只滚动一次.下面是我的代码
- 连续执行的代码