我有一個只有兩件事情的腳本頁:改變顏色或背景,並保持一個時間計數器。有某處存在內存泄漏。關閉和內存泄漏
的的jsfiddle是here
這是腳本:
var TheDateToday = new Date();
var TheColorEffect = (function() {
var TheColorArray = ['red', 'blue', 'purple'];
return function() {
var TheColor = Math.floor(Math.random() * 3);
var TheOpacity = (Math.floor(Math.random() * 20) + 80)/100;
document.getElementById('TheText').style.color = TheColorArray[TheColor];
document.getElementById('TheText').style.opacity = TheOpacity;
document.getElementById('ThePanel').style.backgroundColor = TheColorArray[TheColor];
document.getElementById('ThePanel').innerText = TheDateToday.toTimeString();
};
}());
var TheDateIncrement = (function() {
return function() {
TheDateToday.setSeconds(TheDateToday.getSeconds() + 10); };
}());
setInterval(TheDateIncrement, 10000);
setInterval(TheColorEffect, 2000);
,這是CSS/HTML:
#TheText{
font-size:20px;
font-family:Verdana;
margin:20px 20px;
-webkit-transition:color 2s ease;
-moz-transition:color 2s ease;
-o-transition:color 2s ease;
transition:color 2s ease;}
#ThePanel{
height:100px;
width:200px;
clear:both;
color:white;
padding:20px 20px;
background:red;
-webkit-transition:background 2s ease;
-moz-transition:background 2s ease;
-o-transition:background 2s ease;
transition:background 2s ease;}
<div id="TheText">This is a test text</div>
<div id="ThePanel"></div>
當你在Chrome到時間線,並期待在內存使用情況,它只是上升。如果您在單獨的選項卡中打開這些頁面中的幾個,則CPU可以達到90%的使用率。
哪裏泄漏? 感謝您提出解決此問題的建議。
我相信這個問題可能是每次調用都返回一個新的函數。 – GGJ 2012-03-14 11:59:21