I’m having a problem with a javascript that randomly creates background divs on a page (to create these floating circles on the background of my site). The script runs on an interval:
var floatInterval = window.setInterval(createFloaters, 10000);
function createFloaters()
{
lastHeight = 0;
pageOffset = $('#logo').offset();
for(j = 0; j<10; j++)
{
randomSize = j * Math.random()*20;
randomTop = Math.random()*randomSize + lastHeight + Math.random()*10 + 10;
plusOrMinus = [-1,1][Math.random()*2|0];
randomColorNum = Math.floor(Math.random()*colArr.length);
randomColor = colArr[randomColorNum];
randomLeft = Math.random()*990 + pageOffset.left;
randomOpacity = Math.random()*.5;
$('#floatContainer').append('<div class="circle d' + j + '" style="width:' + randomSize + 'px; height:' + randomSize + 'px; position: absolute; top:' + eval((randomTop - Math.random()*500)*plusOrMinus) + 'px; left:' + eval((randomLeft + Math.random()*200)*plusOrMinus) + 'px; border-radius:' + randomSize + 'px; background:' + randomColor + '"></div>');
lastHeight += randomSize;
$('.d' + j).delay(Math.random()*500).animate({top: randomTop, left: randomLeft, opacity: randomOpacity}, Math.random()*5000 + 5000).fadeOut().queue(function(){
$(this).remove();
});
}
console.log('FLOAT LENGTH: ' + $('#floatContainer div.circle').length);
}
Once a floating circle is created, it animates and then is removed. It appears that the divs aren’t being completely removed.
When I traced out the number of divs being created while focused on the tab I get something normal.
Number of Divs: 13
Number of Divs: 21
Number of Divs: 13
Number of Divs: 21
Number of Divs: 13
Number of Divs: 21
Then I switch tabs, sit there for a few minutes, return to the original tab and this is what I see:
Number of Divs: 187
Number of Divs: 197
Number of Divs: 207
Number of Divs: 13
Number of Divs: 21
Number of Divs: 13
Number of Divs: 21
Number of Divs: 13
Number of Divs: 21
What can I do to correct this issue? How do you delete the dynamically created divs permanently from memory? I checked the activity monitor and the page continues to hog memory the longer it runs.
You are using queue wrong. Just add a callback to fadeOut.