jQuery Memory Leak Suspicion

前端 未结 4 2085
清酒与你
清酒与你 2020-12-11 05:54

I am making an AJAX request for XML. I am doing this every second. I notice that my memory usage grows into the hundreds of megabytes. As you might imagine, the customer is

4条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-11 06:38

    Your timeout function

    setTimeout(self.timeoutHandler, 750);
    

    may be causing memory leak. Although the success: callback is able to exit after finished, there may not be an empty context in a setTimeout callback causing a circular loop. Youl should take out the anonymous function to end up something like this:

    var pManager = new PageManager ();
    pManager.timeoutHandler();
    
    function PageManager () {
    var ret  = (function(self) { 
        return function() { self.timeoutHandler(); };
    })(this);
    
    this.timeoutHandler = function () {
        $.ajax ({
            url: '/echo/json/',
            type: 'post',
            cache: false,
            success: function (data) {
                setTimeout (ret, 750);
            }
        });
    };
    }
    

    This should only call back once.

提交回复
热议问题