[英]How can I get this code to continuously loop through the items?
I have the following code: 我有以下代码:
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);
});
}
});
});
}
What I would like to happen is it puts the 1st element in the design-tips-content div, then wait 5000 seconds, then put the 2nd, then put the 3rd, then loop back to the first element. 我想发生的是将第一个元素放入design-tips-content div中,然后等待5000秒,然后将第二个元素放入,然后将第3个元素放入,然后循环回到第一个元素。 How would I go about doing that?
我将如何去做? Right now it just seems like it is just showing the last element.
现在看来,它只是显示最后一个元素。
Note: I tried to create a jsfiddle for this ( http://jsfiddle.net/allisonc/c8RLZ/ ) but I get the error: 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.
注意:我尝试为此创建一个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.
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.
This code inside the success function should work. 成功函数中的这段代码应该起作用。 It works by building an array of the items' text, and storing the current index value in a variable.
它的工作方式是建立一个由项目文本组成的数组,并将当前索引值存储在一个变量中。 Then, it uses
setInterval
to continually loop through all the items. 然后,它使用
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);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.