A problem to note when using jquery AJAX features (memory overflow) _jquery

Source: Internet
Author: User
Recently a buddy is doing an Ajax long connection project, the page needs to maintain long connection with the server, and after the connection timeout needs to request the connection again, in the process he asked me to use what, I also wanted not to tell him to use jquery. jquery does not have ajaxsuccess ajaxerror these objects, if the request is completed or request failed after the request to be again not good.

But then he told me that he didn't use JQuery to write his own manual XMLHttpRequest. He told me that the beginning was written in jquery, and that there were no problems during the test. But then inadvertently found that when the page opened for a long time, the browser resources unexpectedly occupy a very high cause of insufficient memory and crashes. Later, the bag analysis found that each jquery Ajax request will create a XMLHttpRequest object, in theory, long connection request is an infinite recursion, the request quantity is very large, but because each request will create a new XMLHttpRequest, And jquery does not automatically recycle resources, so it causes a memory overflow.

By looking at the jquery API, we found that jquery also has a complete object, which is called after the request completes, after the request is completed. At the same time there are two parameters XMLHttpRequest, Textstatus. So, all we need to do is to manually recycle the returned XMLHttpRequest objects after the request is complete, as follows:
Copy Code code 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}
});
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.