AJAX和自动刷新

时间:2011-09-10 17:06:56

标签: php javascript html ajax

我试图用AJAX每1秒刷新一次html,我用自己找到的来自不同网站的比特制作了这个代码。我试图了解它是如何工作的。

我希望能够刷新页面而无需在浏览器中重新加载它,我想要JS函数AJAXdisplay();使用我发送给AJAXreturn()的变量每秒运行一次;我打电话的时候。

当我调用AJAXreturn()时;我想让它运行AJAXdisplay();曾经打印出我的php文件中的html,如果索引文件我想要这样的话就在我身上

 <body onClick=:AJAXdisplay(same variables as used when the page was made);">             

</body>    

这是我的代码:

function getHTTPObject(){
if (window.ActiveXObject) {
    return new ActiveXObject("Microsoft.XMLHTTP");
}
if (window.XMLHttpRequest){
    return new XMLHttpRequest();
}
else {
    alert("Your browser does not support AJAX.");
    return null;
}
}   

function AJAXsend(url) {
httpObject = getHTTPObject();
if (httpObject != null) {
    httpObject.open("POST",url);
    httpObject.send(null);
}
}

function AJAXreturn(url,pageName){
httpObject = getHTTPObject();
if (httpObject != null) {
    if (navigator.appName != "Microsoft Internet Explorer") {
        history.replaceState("", "", "index.php?page=" + pageName)
    }
    httpObject.open("POST",url);
    httpObject.send(null);
    AJAXdisplay(httpObject,url,pageName);
}
}

function AJAXdisplay(httpObjectIn,urlIn, pageNameIn){
httpObjectIn.onreadystatechange = function(){
    if(httpObjectIn.readyState == 4){
        document.getElementById('outputHTML').innerHTML = httpObjectIn.responseText;
        AJAXdisplay('function(httpObjectIn,urlIn,pageNameIn)',1000);
    }
}
}

1 个答案:

答案 0 :(得分:2)

要刷新javascript,您应该使用setInterval();功能。这就是你要找的东西:

var timer = setInterval ("AJAXdisplay(variable);", 1000);

如果您需要停止使用的刷新:

clearInterval (timer);