Ajax 回调函数

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

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值