包装的XMLHttpRequest函数的内存泄漏 [英] Memory-leak at a wrapped XMLHttpRequest function

查看:305
本文介绍了包装的XMLHttpRequest函数的内存泄漏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了以下内容:

function ao(){
this.count=0;
this.flag=0;
this.tmr=0;
var self = this;
this.make=function(){
    //log("before: "+this.url+" "+this.xhr);
    self.xhr = (window.XMLHttpRequest)
        ? new XMLHttpRequest() : new ActiveXObject('Microsoft.XMLHTTP');
    //log("after: "+this.xhr);
}
this.request = function (method, url, sendStr, delay){
    this.delay=delay;
    if(delay && self.tmr==0){
        self.start();
    }
    if(self.flag==0){
        this.method = method;
        this.url = url;
        this.sendStr = sendStr;
        self.make();
        this.xhr.open(method, url, true);
        this.xhr.onreadystatechange = this.stateChange;
        this.xhr.onabort=this.rrr;
        this.xhr.onerror=this.rrr;
        this.xhr.setRequestHeader("Cache-Control","no-cache");
        this.xhr.send(sendStr);
    }
};
this.repeat=function(){
    if(this.flag==0){
        this.flag=1;
        this.count++;
        this.xhr.open(self.method, self.url+"?"+this.count, true);

        this.xhr.onreadystatechange = this.stateChange;
        this.xhr.onabort=this.rrr;
        this.xhr.onerror=this.rrr;
        this.xhr.setRequestHeader("Cache-Control","no-cache");

        this.xhr.send(self.sendStr);
    }
    return 0;
}
this.stop=function(){
    window.clearInterval(this.tmr);
    this.tmr=0;
    this.flag=0;
}
this.start =function(){
    self.tmr=window.setInterval(function(){self.repeat();},self.delay);
}
this.stateChange = function(){
    if (self.xhr.readyState <= 1){
        return;
        self.log("404 errors");
    } else {
        if (self.xhr.readyState == 4 && self.xhr.status == 200){
            self.resp = self.xhr.responseText;
            if (self.callback != null)
                self.callback(self.xhr.readyState, self.xhr.status);
            else {
                if (self.getHTML) {
                    self.getHTML(self.resp);
                    this.xhr=null;
                } else {
                    if (self.xhr.readyState == 4 && self.xhr.status == 200){
                        self.parseJSON();
                        self.traverse();
                        this.ro=null;
                        this.xhr=null;
                    }
                }
            }
        }
    }
    self.flag=0;
    return 0;
};

并且在Windows ff中有内存泄漏.我花了几天的时间试图修复它,但是我很沮丧.

and in windows ff there is a memory leak. I spent days trying to fix it, but I'm stumped.

以下作品:

var x=new ao();
ao.request("POST","/cgi-bin/sdf.cgi","text",1000)

每隔1000毫秒(如果完成了先前的请求),它将发出新的请求.

and after every 1000 miliseconds if previous request is done, it makes new request.

推荐答案

在使用 XMLHttpRequest对象的onreadystatechanged事件.如果处理程序 是一个闭包,它关闭对同一XMLHttpRequest的引用 对象,可以创建另一个循环依赖项.这不是 由于该对象不是部件,因此必须由上述工具进行检测 的DOM. 链接

Developers should also take precautions when it comes to using the onreadystatechanged event of an XMLHttpRequest object. If the handler is a closure that closes over a reference to the same XMLHttpRequest object, another circular dependency can be created. This isn't necessairly detected by the above tool because the object is not part of the DOM. Link

这篇关于包装的XMLHttpRequest函数的内存泄漏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆