2014-01-08 27 views
1

我的代码首先产生以下两行:线消失

 
1,2,3,4,5 
1,2,3,4 

然后它产生这些线,并且在这两个消失:

 
1,2,3 
1,2 
1 

这究竟是为什么?

var arr=[1,2,3,4,5]; 

document.write(arr+"</br>"); 

function popit() { 
    if(arr.length>0) { 
     arr.pop(); 
     if(arr.length<=0) { 
      clearTimeout(p); 
      document.write("end"); 
     } 
     var p=setTimeout(popit,1000); 
     document.write(arr); 
     document.write("</br>"); 
    } 
} 
window.onLoad=popit(); 
+0

入住这里http://stackoverflow.com/questions/802854/why-is-document-write-considered-a-bad-practice – elclanrs

回答

1

当您使用document.write()后的页面加载完成后,你是,通过使用window.onLoad=popit(),这将从根本上清除体内的HTML和开始添加新的内容。请勿使用document.write()。如果必须输出到屏幕,请添加到document.body.innerHTML

或者,你可以只console.log(arr)