Home  >  Article  >  Web Front-end  >  jquery does not automatically recycle the xmlHttpRequest object, causing memory overflow_jquery

jquery does not automatically recycle the xmlHttpRequest object, causing memory overflow_jquery

WBOY
WBOYOriginal
2016-05-16 17:52:441005browse

I have never noticed this before. Fortunately, I read kuibono’s article today. Here is the code snippet given by kuibono to manually recycle the xmlHttpRequest object:
Every Ajax request of jquery will create an xmlHttprequest object. Theoretically, The request for a long connection is an infinite recursion, and the number of requests is very large. However, since each request will create a new xmlhttprequest, and jquery will not automatically recycle resources, it will cause memory overflow.

By looking at the jquery API, I found that jquery also has a complete object, which is a callback function after the request is completed (called after the request is successful or failed). There are two parameters XMLHttpRequest and textStatus at the same time. Therefore, we only need to manually recycle the returned XMLHttprequest object after the request is completed. The code is as follows:

Copy code The code is as follows:

$.ajax({
url: "http://www.jb51.net",
data: { name: "xxxx" },
dataType: "xml",
success: function (data, textStatus) {
//do something...
},
complete: function (XHR, TS) { XHR = null }
});
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn