JQuery ajax memory leak error

I wonder if a memory leak could occur in the following jQuery pseudo-code (since the success callback always has a link to this)?

var _this = this; $.ajax({ url: "foo", type: "POST", data: data, success: function() { // Do stuff with _this _this.doStuffs(); }) 
+4
source share
2 answers

Is this code inside a function?

If so (which I assume), then it will be inaudible to collect garbage right after the completion or unsuccessful ajax call. Therefore, in theory there is no reason to worry about a memory leak here.

+1
source

I do not see a memory leak there. The success callback must be collected from the garbage after it is called (must be implemented correctly, you have this right), but it is executed correctly by jQuery and browsers, possibly even in IE). After that, it would also be possible to assemble the _this object (if not referenced from outside) - but not earlier, of course, that the goal is closure.

0
source

Source: https://habr.com/ru/post/1444214/


All Articles