0
function AjaxRequest(params, url) {
    if (params) {
        this.params = params;
        this.type = "GET";         
        this.url = url;
//      this.contentType = "multipart/form-data";
        this.contentLength = params.length;;
    }
}

AjaxRequest.prototype.createXmlHttpObject = function() {
    try {
        this.xmlHttp = new XMLHttpRequest();
    }
    catch (e) {
        try {
            this.xmlHttp = new ActiveXObject("Microsoft.XMLHttp");
        }
        catch (e) {}
    }

    if (!this.xmlHttp) {
        alert("Error creating XMLHttpRequestObject");
    }
}

AjaxRequest.prototype.process = function() {
    try {
        if (this.xmlHttp) {
            document.getElementById("loading"+this.params).innerHTML = "loading...";
            document.getElementById("loading"+this.params).className = "loading";
            this.xmlHttp.onreadystatechange = this.handleRequestStateChange();
            this.xmlHttp.open(this.type, this.url, true);
            this.xmlHttp.setRequestHeader("Content-Type", this.contentType);
            this.xmlHttp.setRequestHeader("Content-Length", this.contentLength);
            this.xmlHttp.send(this.params);
            }
        }
        catch (e) {
            document.getElementById("loading"+this.params).innerHTML = "";
            alert("Unable to connect to server");
        }
    }

AjaxRequest.prototype.handleRequestStateChange = function() {
    var self = this;
    return function() {
        try {
            if (self.xmlHttp.readyState == 4 && self.xmlHttp.status == 200) {
                self.handleServerResponse();
            }
        }
        catch (e) {
            alert(self.xmlHttp.statusText);
        } 
    };
}

AjaxRequest.prototype.handleServerResponse = function() {
    try {
        if(this.xmlHttp.responseText.length > 0){
            document.getElementById("loading"+this.params).innerHTML = this.xmlHttp.responseText;
        }
    }
    catch (e) {
        alert("Error reading server response");
    }
}

function CreateAjaxControl(params, url){
    var con = $("#"+params+" select").val();
    url += "?id="+params+"&con="+con;
    var ajaxRequest = new AjaxRequest(params, url);       
    ajaxRequest.createXmlHttpObject();
    ajaxRequest.process();
    ajaxRequest.count = 0;
    ajaxRequest.progress = CheckingProgress;
    ajaxRequest.progress(ajaxRequest, ajaxRequest.params, ajaxRequest.count);
    //var ajaxRequest = new AjaxRequest(params, url);
    //ajaxRequest.checking = setInterval(function(){CheckingProgress(ajaxRequest.params);}, 100);

}

//function Check(id){
//    var res = 0;
//    while( res != "done..."){
//        res = CheckingProgress(id, res);
//    }
//}


function CheckingProgress(obj, id, count){
    var self = obj;
    if (window.XMLHttpRequest){
        xmlhttp8 = new XMLHttpRequest();
    }else{
        xmlhttp8 = new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp8.onreadystatechange = function(){
        if (xmlhttp8.readyState==4 && xmlhttp8.status==200){
            var result = xmlhttp8.responseText.split(',');
            document.getElementById("loading"+id).innerHTML = result[0] + " out of " + result[1];
            self.count = result[0];
            self.progress(self, self.params, self.count);
        }else if(xmlhttp8.status==404){
            document.getElementById("loading"+id).innerHTML = "done...";
            //return "done";
        }
    }
    xmlhttp8.open("GET","views/test2.php?id="+id+"&count="+count,true);
    xmlhttp8.send();
}

我使用一个长轮询脚本来检查数据库中更新记录的数量,当还没有更新时它可以完美运行,但是当我更新数据库中的记录时,会创建一个新的 ajax 请求,并将新计数作为参数,并且第一个请求仍然保持不变。这里似乎有什么问题?这是我将长轮询方法附加到对象的方式吗?请帮助。

4

1 回答 1

0

CheckingProgress() 中的参数计数有什么意义?每次调用 CheckingProgress() 时,obj.count 与传入的参数 count 相同,因此函数末尾对 count 的引用可以替换为 self.count

于 2013-10-10T00:45:17.333 回答