解决IE6内存泄露的另类方法
2011-05-17 16:35
162 查看
Hedger Wang 在国内 blog 上得到的方法:使用 try … finally 结构来使对象最终为 null ,以阻止内存泄露。其中举了个例子: function createButton() { var obj = document.createElement("button"); obj.innerHTML = "click me"; obj.onclick = function() { //handle onclick } obj.onmouseover = function() { //handle onmouseover } return obj;//return a object which has memory leak problem in IE6 } var dButton = document.getElementsById("d1").appendChild(createButton()); //skipped....对于 IE6 中,引起内存泄露的原因,可看《Understanding and Solving Internet Explorer Leak Patterns》一文。上面的例子,应该属于上文中的 “Closures”原因。 再看下用 try … finally 的解决方法: /** * Use the try ... finally statement to resolve the memory leak issue */ function createButton() { var obj = document.createElement("button"); obj.innerHTML = "click me"; obj.onclick = function() { //handle onclick } obj.onmouseover = function() { //handle onmouseover } //this helps to fix the memory leak issue try { return obj; } finally { obj = null; } } var dButton = document.getElementsById("d1").appendChild(createButton()); //skipped....可能大家有疑问: finally 是如何解析的呢?答案是:先执行 try 语句再执行 finally 语句。例如: function foo() { var x = 0; try { return print("call return " + (++x)); } finally { print("call finally " + (++x)); } } print('before'); print(foo()); print('after'); 返回的结果为: print » before print » call return 1 print » call finally 2 print » true print » after
相关文章推荐
- 另类方法解决IE6不支持fixed
- IE6中用了float:left之后导致margin-left双倍边距的BUG解决方法
- IE6 双倍边距的解决方法
- IE6 png 透明 (三种解决方法)(转来的哦)
- 解决 min-width 在 IE6 中无效的方法
- IE6下图片下方有空间距的多种解决方法
- bootstrap支持ie8 让IE6 IE7 IE8 IE9 IE10 IE11支持Bootstrap的解决方法
- IE6 7下解决无内容的空DIV占位置的方法
- ie6下不支持png格式的解决方法
- IE6-IE9不支持table.innerHTML的解决方法分享
- IE6完美解决fixed方法
- IE6下图片下方有空隙的解决方法
- IE6 png图片bug的解决方法
- 在IE6中无法正确处理a标签伪类的原因及解决方法
- IE6不支持position:fixed的解决方法
- 解决IE6下PNG图片背景不透明的三种方法(CSS/JS/JQuery)
- IE6不支持position:fixed的解决方法
- 安装IE6时“以前的安装有尚未完成的操作,需要重新启动”的解决方法
- IE6下PNG透明图片背景显示灰色的完美解决方法
- 在ie6中DIV最小高度的解决方法