我一直试图让我的代码开始,但我遇到了所有浏览器中不断出现的错误,告诉我我的ajax对象不包含open函数。我确定我输入的内容是错误的,但请原谅我,因为JavaScript不是我的强项:)
window.onload = function(){init();}
function init() {
ajax = ajaxInit();
ajax.onreadystatechange = update(ajax);
ajaxContact(ajax);
setInterval("ajaxContact('"+ajax+"')",5000);
}
function ajaxInit() {
if (window.XMLHttpRequest) {
ajax = new XMLHttpRequest();
}
else {
if (window.ActiveXObject) {
ajax = new ActiveXObject("Microsoft.XMLHTTP");
}
}
if (ajax) {
document.getElementById("status").innerHTML = "AJAX initialized";
return ajax;
}
else {
docuement.getElementById("status").innerHTML = "Error: AJAX not available";
return false;
}
}
function ajaxContact(ajax) {
try {
ajax.open("GET","updateAjax.php?" + "ran=" + Math.random(),true);
ajax.send();
}
catch (err) {
alert(err.message);
document.getElementById("status").innerHTML = "Error contacting server";
document.getElementById("loading").src = "images/redx.png";
}
}
function update(ajax) {
if (ajax.readyState==4 && ajax.status==200){
dataObj = jsonTranslate(ajax);
document.getElementById("status").innerHTML = dataObj.status;
document.getElementById("frame").innerHTML =
"Frame:" + dataObj.firstFrame + "/" + dataObj.lastFrame;
document.getElementById("thumbnail").src = dataObj.imgSrc;
}
if (ajax.status==404) {
document.getElementById("status").innerHTML = "Ajax updater not found";
document.getElementById("loading").src = "images/redx.png";
}
}
function jsonTranslate(ajax) {
return eval('(' + ajax.responseText + ')');
}
答案 0 :(得分:3)
您将ajax
变量作为字符串传递...
setInterval("ajaxContact('"+ajax+"')",5000);
尝试用...替换它。
setInterval(function() { ajaxContact(ajax); }, 5000);