如何使用ajax自動刷新頁面的一部分,而無需在JavaScript內手動調用函數?
var xmlhttp;
//Set up ajax first so he knows which guy to play with
function loadXMLDoc(url,cfunc)
{
//Code to catch modern browsers
if (window.XMLHttpRequest)
{
xmlhttp=new XMLHttpRequest();
}
//Code to catch crap browsers
else
{
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
//Set up
xmlhttp.onreadystatechange=cfunc;
xmlhttp.open("GET",url,true);
xmlhttp.send();
}
//Set a function to deploy when something calls myFunction()
function myFunction()
{
loadXMLDoc("../../../support/ajaxTest.txt",function()
{
//Fires off when button pressed
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("statusRefresh").innerHTML=xmlhttp.responseText;
setInterval("alert('Hello I did something but i needed to be invoked by a button first')", 5000);
}
});
}
我想打電話給寧靜的Java服務刷新 '狀態'。一旦頁面被擊中,我需要ajax來自動刷新這個狀態。刷新方法不是即時的,因爲它必須與其他機器交談。
那麼實際上會觸發autoRefresh?只需添加document.onload = autoRefresh;在JavaScript範圍內會做到這一點? – stackoverflow 2011-06-10 16:47:27