When using the jQuery Ajax function, you need to pay attention to a problem (memory overflow)

Source: Internet
Author: User

Recently, a buddy is working on an Ajax persistent connection project. The page needs to maintain a persistent connection with the server and request a connection again after the connection times out. During the process, he asked me what to use, I also told him to use jQuery without thinking about it. Isn't jQuery an ajaxSuccess ajaxError object? It's okay to send a new request after the request is complete or fails.

But then he told me that jQuery was not used and he wrote XMLhttprequest manually. He told me that jquery was used in the beginning and there was no problem during the test. However, when the page was opened for a long time, the browser resource usage was very high, leading to insufficient memory and crash. Later, the packet capture analysis found that each jquery Ajax request will create an xmlHttprequest object. Theoretically, a persistent connection request is an infinite recursion and the number of requests is very large, however, each request creates a new xmlhttprequest, and jquery does not automatically Recycle resources, resulting in memory overflow.

By viewing the jquery API, it is found that jquery also has a complete object, which is a callback function after the request is complete (called after the request is successful or fails ). There are also two parameters: XMLHttpRequest and textStatus. Therefore, you only need to manually recycle the returned XMLHttprequest object after the request is complete. The Code is as follows:Copy codeThe 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}
});

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.