What about jquery memory leaks? The jquery memory leak solution Tutorial

Source: Internet
Author: User

This article we share the jquery memory leak solution, for your reference, the specific contents are as follows

Idea: The way to remove jquery element objects for the jquery extension greatly reduces the pressure on memory leaks

;(function ($) {
if (!$.lui.widget) $.lui.widget = {};
$.lui.newguid () generates a random 32-bit ID
If the jquery object is generated multiple times in this way, the HTML code string will be repeated over and over in memory, consuming additional content and leaking. and $ ("<span></span>"). attr (' id ', _id) does not have this problem.
$.lui.widget.__clean$ = $ ("<span id= '" + $.lui.newguid () + "' ></span>");
/**
* Free jquery object, no return value. This method is used to solve the memory leak problem of jquery
*/
$.fn.del =function (Selector, keepdata) {
if (!selector | | $.filter (SELECTOR, [this]). length) {
Releasing a DOM object
Varitem = $ (this);
Varclearitem = $.lui.widget.__clean$;
Item.appendto (Clearitem);
$ (' * ', clearitem). Each (function (I, e) {
(Events = $.data (this, ' events ')) && $.each (Events,function (i, E1) {
$ (E). Unbind (i + '. * ');
});
$.event.remove (this);
$.removedata (this);
});
clearitem[0].innerhtml = ';
Item =null;
}
};
/**
* Calculates the length of a string that is not broken on an element
* @param {jquery} $Element jquery elements
* @param {string} str string
* @returns {Number} string length (px)
*/
$.lui.widget.clacstrlength =function ($Element, str) {
var_id = $.lui.newguid ();
Var$tmpspan = $ ("<span></span>"). attr (' id ', _id). CSS ({
' Position ': ' absolute ',
' Top ': ' -1000px '
}). Appendto (' body ');
Var_width = $tmpSpan. css ({
' font-family ': $Element. css (' font-family '),
' Font-size ': $Element. css (' font-size '),
' letter-spacing ': $Element. css (' letter-spacing '),
' word-spacing ': $Element. css (' word-spacing '),
' Text-indent ': $Element. css (' text-indent ')
}). text (str). Innerwidth ();
$tmpSpan. del ();
$tmpSpan =null;
Return_width;
};
/**
* On an element on a pixel intercept string (using the browser default processing whitespace, not for complex scenes, only in the case of a pixel-by-line interception of strings)
* @param $Element jquery elements (the font settings for this element must already be determined)
* @param str string
* @param limit pixel value
* @returns the string captured by pixel
*/
$.LUI.WIDGET.SUBSTRBYPX =function ($Element, Str,limit) {
if ($Element = = Undefined | |! $Elementinstanceof $) return ';
if (str = = Undefined | | Typeofstr!= ' string ') return ';
if (limit = = undefined) returnstr;
if (!str | | limit <=0) return ";
Var_str =newstring (str);
var_id = $.lui.newguid ();
Var$tmpspan = $ ("<span></span>"). attr (' id ', _id). CSS ({
' Position ': ' absolute ',
' Top ': ' -1000px '
}). Appendto (' body ');
Var_width = $tmpSpan. css ({
' font-family ': $Element. css (' font-family '),
' Font-size ': $Element. css (' font-size '),
' letter-spacing ': $Element. css (' letter-spacing '),
' word-spacing ': $Element. css (' word-spacing '),
' Text-indent ': $Element. css (' text-indent ')
}). text (str). Innerwidth ();
while (_width >limit) {
_str = _str.substring (0, _str.length-1);
_width = $tmp. Text (_STR). Innerwidth ();
}
$tmpSpan. del ();
$tmpSpan =null;
RETURN_STR;
};
}) (JQuery);

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.