别的不说了、直接上干货了、看代码
var xmlHttp;function createXmlHttpRequest(){
if (window.XMLHttpRequest) {// Firefox,Opera 8.0+,Safari
xmlHttp = new XMLHttpRequest();
}else if (window.ActiveXObject) { //IE
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}else{
xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
}
}
function dostart(){
createXmlHttpRequest();
var url="DynamicUpdate.aspx?task=reset";
xmlHttp.open("GET",url,true);
xmlHttp.onreadystatechange=startCallback;
xmlHttp.send(null);
}
function startCallback(){
if(xmlHttp.readyState==4){
if(xmlHttp.status==200){
setTimeout("pollServer()",5000);
refreshTime();
}
}
}
function pollServer(){
createXmlHttpRequest();
var url="DynamicUpdate.aspx?task=foo";
xmlHttp.open("GET",true);
xmlHttp.onreadystatechange=pollCallback;
xmlHttp.send(null);
}
function pollCallback(){
if(xmlHttp.readyState==4){
if(xmlHttp.status==200){
var message=xmlHttp.responseXML.getElementsByTagName("message")[0].firstChild.data;
if(message!="done")
{
var new_row=createRow(message);
var table=document.getElementById("dynamicUpdateArea");
var table_body=table.getElementsByTagName("tbody").item(0);
var first_row=table_body.getElementsByTagName("tr").item(1);
table_body.insertBefore(new_row,first_row);
setTimeout("pollServer()",5000);
refreshTime();
}
}
}
}
function createRow(message){
var row=document.createElement("tr");
var cell=document.createElement("td");
var cell_data=document.createTextNode(message);
cell.appendChild(cell_data);
row.appendChild(cell);
return row;
}
function refreshTime(){
var time_span=document.getElementById("time");
var time_val=time_span.innerHTML;
var int_val=parseInt(time_val);
int_val--;
if(int_val>-1){
setTimeout("refreshTime()",1000);
time_span.innerHTML=int_val;
}
else{
time_span.innerHTML=5;
}
}
有什么、一看代码就知道了、别的都不多说了、
jquery中、有对这个的二次封装、使用起来很好。
原文链接:https://www.f2er.com/ajax/166104.html