addEventListener内存泄漏 [英] addEventListener memory leaks

查看:686
本文介绍了addEventListener内存泄漏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通过 注册活动时对一个元素添加addEventListener ,然后在不删除该事件的情况下删除该元素,并重复这样做,内存会被泄露吗?

When registering an event via addEventListener on an element, then delete that element without removing the event, and doing so repeatedly, would memory be "leaked"?

推荐答案

它不应泄漏。当事件处理程序导致主机对象<> JS-对象循环时,一个臭名昭着的漏洞就像IE(最高版本7)和IE(最高版本8)不支持 addEventListener

It shouldn't leak. The one browser that's infamous for leaking like hell when an event handler causes a host-object<>JS-object loop is IE (up to version 7), and IE (up to version 8) doesn't support addEventListener.

如果要在一个浏览器中测试它,请保持此运行并长期看看浏览器的内存使用情况如何受到影响特别浏览器。

Leave this running and see how the browser's memory usage is affected in the long term, if you want to test it in a particular browser.

<div id="x"></div>
<script type="text/javascript">
    function replace() {
        var x= document.getElementById('x');
        if (x.firstChild!==null)
            x.removeChild(x.firstChild);
        var el= document.createElement('p');
        el.addEventListener('click', click, false);
        x.appendChild(el);
    }
    function click() {
        alert('click');
    };
    setInterval(replace, 1);
</script>

(要使用参考循环进行测试,请移动功能单击定义到替换正文。)

(To test it with a reference loop present, move the function click definition up into the replace body.)

这篇关于addEventListener内存泄漏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆