我希望在固定的时间间隔内读取XML文档,并在每次查找更新时避免完整页面刷新。下面的代码被注释掉了,但却搞乱了JQuery循环。 (基于http://buildinternet.com/project/totem/) 需要一些帮助。
//function refresh()
// {
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
myFunction(this);
}
};
xhttp.open("GET", "drift.xml", true);
xhttp.send();
// }
function myFunction(xml) {
var xmlDoc = xml.responseXML;
var item = xmlDoc.getElementsByTagName("item");
var itemlength = item.length;
var i;
var html='';
var today = new Date();
var aktuella = [];
$("#wrapper").append('<ul id=\'wrap\'></ul>');
for (i = 0; i < itemlength; i++) {
var postDate = xmlDoc.getElementsByTagName("expires")[i].childNodes[0].nodeValue;
var date = postDate.substring(8, 10);
var month = postDate.substring(5, 7);
var year = postDate.substring(0, 4);
var expiredDate = new Date(year, month - 1, date);
if (expiredDate > testDate) {
aktuella.push(i);
}
};
if (aktuella.length == 0 ) {
var none = "<p>Info</p>";
$('#wrapper').prepend(none);
};
if (aktuella.length == 1) {
var expires = '<p>' + xmlDoc.getElementsByTagName("expires")[0].parentNode.childNodes[1].textContent + '</p>';
$('#wrapper').prepend(expires);
};
if (aktuella.length > 1) {
for (i = 0; i < aktuella.length; i++) {
var expires = '<li id="' + i + '">' + xmlDoc.getElementsByTagName("expires")[i].parentNode.childNodes[1].textContent + ' - Se Driftinfo</li>';
$('#wrap').prepend(expires);
};
};
$(function(){
$('#wrap').totemticker({
row_height : '120px',
next : '#ticker-next',
previous : '#ticker-previous',
stop : null,
start : null,
mousestop : true,
});
});
}
// refresh();
// setInterval("refresh()", 5000);
</script>
答案 0 :(得分:0)
简单: 添加这个你的脚本:
setInterval(function(){
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
myFunction(this);
}
};
}, milliseconds);
答案 1 :(得分:0)
现在尝试一下。那里有一个错误。功能代替功能和调用函数后的间隔
function myFunction(xml) {
var xmlDoc = xml.responseXML;
var item = xmlDoc.getElementsByTagName("item");
var itemlength = item.length;
var i;
var html='';
var today = new Date();
var aktuella = [];
$("#wrapper").append('<ul id=\'wrap\'></ul>');
for (i = 0; i < itemlength; i++) {
var postDate = xmlDoc.getElementsByTagName("expires") [i].childNodes[0].nodeValue;
var date = postDate.substring(8, 10);
var month = postDate.substring(5, 7);
var year = postDate.substring(0, 4);
var expiredDate = new Date(year, month - 1, date);
if (expiredDate > testDate) {
aktuella.push(i);
}
};
setInterval(function(){
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
myFunction(this);
}
};
xhttp.open("GET", "drift.xml", true);
xhttp.send();
}, milliseconds);
答案 2 :(得分:0)
以下是没有尝试刷新ajax的网站:
http://socialsiberia.com/dash/test1.html
以下是我添加以下代码时出现的奇怪行为:
http://socialsiberia.com/dash/test2.html
setInterval(function(){
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
myFunction(this);
}
};
xhttp.open("GET", "test.xml", true);
xhttp.send();
}, 5000);
答案 3 :(得分:0)
var xhttp = new XMLHttpRequest();
function myFunction(xml) {
var xmlDoc = xml.responseXML;
var item = xmlDoc.getElementsByTagName("item");
var itemlength = item.length;
var i;
var html='';
var today = new Date();
var aktuella = [];
$("#wrapper").append('<ul id=\'wrap\'></ul>');
for (i = 0; i < itemlength; i++) {
var postDate = xmlDoc.getElementsByTagName("expires")[i].childNodes[0].nodeValue;
var date = postDate.substring(8, 10);
var month = postDate.substring(5, 7);
var year = postDate.substring(0, 4);
var expiredDate = new Date(year, month - 1, date);
if (expiredDate > testDate) {
aktuella.push(i);
}
};
if (aktuella.length == 0 ) {
var none = "<p>Info</p>";
$('#wrapper').prepend(none);
};
if (aktuella.length == 1) {
var expires = '<p>' + xmlDoc.getElementsByTagName("expires")[0].parentNode.childNodes[1].textContent + '</p>';
$('#wrapper').prepend(expires);
};
if (aktuella.length > 1) {
for (i = 0; i < aktuella.length; i++) {
var expires = '<li id="' + i + '">' + xmlDoc.getElementsByTagName("expires")[i].parentNode.childNodes[1].textContent + ' - Se Driftinfo</li>';
$('#wrap').prepend(expires);
};
};
$(function(){
$('#wrap').totemticker({
row_height : '120px',
next : '#ticker-next',
previous : '#ticker-previous',
stop : null,
start : null,
mousestop : true,
});
});
}
funtion makeinterval(){
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
myFunction(this);
}
};
xhttp.open("GET", "drift.xml", true);
xhttp.send();
}
setInterval(function(){
makeInterval();
}, 5000);
makeinterval()