Try finally to prevent memory leakage

Source: Internet
Author: User
Tags finally block

<textarea id="runcode64629"><P></textarea>
[Ctrl + A select all Note: If you need to introduce external JS, You need to refresh it to execute]

This method causes 100% Memory leakage in IE.

Try finally is easy to solve.CopyCodeThe Code is as follows: function createbutton (){
VaR OBJ = Document. createelement ("button ");
OBJ. innerhtml = "Click me! ";
OBJ. onclick = function (){
// Process the Click Event
}
OBJ. onmouseover = function (){
// Process the Mouseover event
}
Try {
Return OBJ;
} Finally {
OBJ = NULL; // This statement is executed after return, effectively solving the problem of setting OBJ to null after return.
}
}

In a function or method, in fact, there are many places where such an optional return value is required, and finally some things are executed

========================================================== ==================================
Appendix 1: javascript error (try/catch/finally)
Introduction
Like other programming versions, JavaScript provides the possibility to make use
The try/catch/Finally block. Usually when an error is encountered then the script stops and doesn' t
Continue with the rest of the page. The try/catch/Finally block can be used to continue
Processing with the rest of the page. You just have to put the code in your try block and when
An error in encountered there, then it will call the Catch Block. The Finally block is called always
Regardless of an error occurred or not. The following example makes the usage clear.

Example:

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.