设置innerHTML会增加HTML nodecount [英] Setting innerHTML increases HTML nodecount

查看:92
本文介绍了设置innerHTML会增加HTML nodecount的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我注意到在使用 someElement.innerHTML 时,DOM节点数会增加。

So i noticed that on using someElement.innerHTML the DOM Nodes count increases.

我猜参考文件已被杀死但是在垃圾回收器删除对象之前,仍然会分配内存。

I guess that the reference is killed but the memory is still allocated until the garbage collector deletes the object.

示例(HTML):

    <html>
    <head>
        <meta charset="utf-8">
        <link rel="stylesheet" href="test.css">
        <script src="script.js"></script>
    </head>
    <body onload="startTimer()">
        <div id="timeContainer">Time Goes Here</div>
    </body>

</html>

示例(JavaScript):

Example(JavaScript):

 var timer;
 var body;
 var oldTime = "";
 var timeContainer;


 function startTimer(){
    timeContainer = document.getElementById("timeContainer");
    timer = setInterval(getTime, 10);
 }

function getTime(){
    var d = new Date();
    var timeString = d.getUTCHours() +":"+ d.getUTCMinutes(); +":"+ d.getUTCSeconds();

    if(timeString != oldTime){
        oldTime = timeString;
        timeContainer.innerHTML = timeString;
    }
}



到目前为止我尝试了什么




  • 我试图使用 someElement.textContent

  • 我删除了whoe ParentElement每次刷新计时器并创建一个新计时器

  • 当我只刷新内容时,我怎么能避免增加节点数呢?为什么它还需要一个额外的节点呢?

    How can i avoid to increase the node count even once when im just refreshing content and why does it need an extra Node anyways?

    推荐答案


    我猜这个引用已被终止,但内存仍被分配
    ,直到垃圾收集器删除该对象。

    I guess that the reference is killed but the memory is still allocated until the garbage collector deletes the object.

    正确。


    如果只是
    ,我怎么能避免增加节点数令人耳目一新的内容?

    How can i avoid to increase the node count even once when im just refreshing content?

    你不能,也不应该担心它。这是浏览器的域,它在垃圾收集方面做了它想要做的事情(不同的浏览器可能会做不同的事情)。

    You can't, nor should you worry about it. This is the browser's domain, it does what it wants to do when it comes to garbage collection (and different browsers might do it differently).


    我每次刷新计时器时都删除了整个ParentElement,
    创建了一个新的

    I deleted the whole ParentElement everytime it refreshes the timer and created a new one

    只是因为你删除了它(制作它不可访问),并不意味着它是垃圾立即收集。

    Just because you deleted it (made it inaccessible), doesn't mean it was garbage collected instantly.

    这篇关于设置innerHTML会增加HTML nodecount的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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