JQuery has become increasingly popular recently, and more people are using it. JQuery's most powerful highlight is its CSS3 selector and extremely simple Ajax request call.
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:
$. Ajax ({
Url: http://www.bkjia.com,
Data: {name: "xxxx "},
DataType: "xml ",
Success: function (data, textStatus ){
// Do something...
},
Complete: function (XHR, TS) {XHR = null}
});
From kuibono