jQuery Memory Leak Suspicion

前端 未结 4 2087
清酒与你
清酒与你 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条回答
  •  旧时难觅i
    2020-12-11 06:32

    I'm just going to throw my two cents in but i agree jAndy (+1). However, I would generate a single bound callback once, rather than creating a new closure on ever iteration of the callback (which in theory could keep the scope and the xml data alive for a lot longer than you want. I would suggest another alternative like this:

    function PageManager () {
        var callback  = (function(self) { 
            return function() { self.timeoutHandler(); };
        })(this); // only one callback
    
        this.timeoutHandler = function () {
            $.ajax ({
                url: '/echo/json/',
                type: 'post',
                cache: false,
                data: {a:Math.random() },
                success: function (data) {
                    //var xmlDoc = $($.parseXML (data));
                    // Processing of XML
                    //alert("data");
                    setTimeout (callback, 750);
                }
            });
        };
    }
    
    var pm = new PageManager ();
    pm.timeoutHandler();
    

    Edit

    Here is a jsFiddle with the above code, and I watched the memory management for a little while, but not nearly enough to be conclusive in any sense.

提交回复
热议问题