解释问题
所以我注意到在使用someElement.innerHTML时,DOM节点数增加了.
我猜这个引用被杀了,但是在垃圾收集器删除对象之前仍然会分配内存.
实施例(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):
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
题
解决方法
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?
你不能,也不应该担心它.这是浏览器的域,它在垃圾收集方面做了它想要做的事情(不同的浏览器可能会做不同的事情).
I deleted the whole ParentElement everytime it refreshes the timer and
created a new one