1

My code produces these two lines first:

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

Then it produces these lines, and those two disappear:

1,2,3
1,2
1

Why is this happening?

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();
Trojan
  • 2,208
  • 26
  • 40
AL-zami
  • 7,637
  • 11
  • 53
  • 104
  • Check here http://stackoverflow.com/questions/802854/why-is-document-write-considered-a-bad-practice – elclanrs Jan 08 '14 at 22:56

1 Answers1

1

When you use document.write() after the page has finished loading, which you are, by using window.onLoad=popit(), it will essentially clear the HTML of the body and start adding new content. Don't use document.write(). If you have to output to the screen, add to document.body.innerHTML.

Alternatively, you could just console.log(arr)

scrblnrd3
  • 6,366
  • 9
  • 31
  • 61