Try finally to prevent memory leakage
<Div id = "d1"> </div>
<SCRIPT>
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
}
Return OBJ; // the object to be created must be returned, so you cannot set OBJ to null directly. after return, obj is a local variable and cannot be referenced from htmlelement externally. problems will occur in IE
}
VaR button = Document. getelementsbyid ("d1"). appendchild (createbutton ());
Button. Do something ();
Button. Do something ();
........
Something. Something (button );
......
</SCRIPT>
This method causes 100% Memory leakage in IE.
Try finally is easy to solve.
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. The effect solves 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