<html> <head> <title>Memory leak</title> <style> body{ padding: 10px; } </style> </head> <body> </body> <script> var q = []; var n = 0; setInterval(function(){ q.push(makeSpan()); if(q.length>=10){ var s = q.shift(); if(s){ s.parentNode.removeChild(s); } } n++; },10);
function makeSpan(){ var s = document.createElement("span"); document.body.appendChild(s); var t=document.createTextNode("*** " + n + " ***"); s.appendChild(t); s.onclick=function(e){ s.style.backgroundColor="red"; alert(n); }; return s; }; </script> </html>