加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

Ajax 回调函数

发布时间:2020-12-16 00:58:49 所属栏目:百科 来源:网络整理
导读:别的不说了、直接上干货了、看代码 var xmlHttp; function createXmlHttpRequest(){ if (window.XMLHttpRequest) {// Firefox,Opera 8.0+,Safari xmlHttp = new XMLHttpRequest(); }else if (window.ActiveXObject) { //IE xmlHttp = new ActiveXObject("Mic

别的不说了、直接上干货了、看代码

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中、有对这个的二次封装、使用起来很好。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读