Jump to content

problem with setInterval()


freshboy

Recommended Posts

<html><head><title>Untitled Document</title><meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"></head><body><script language="javascript" type="text/javascript">a=new Date()b=new Date()c=1function aa(){document.write(c++)document.write("<BR>")}while(c<100){setInterval(aa(),1000)}</script></body></html>I want to print a column that includes a sequence of numbers and the number on each row will be printed after 1 second.After that, the number will be increased to 1 unit and It happens util c==100However, It didn't work.Could you please help me why it doesn't work !.

Link to comment
Share on other sites

freshboy,You never want to use a "timer" function inside a loop(javascript anyway).The other problem with the code is that the only time "document.write()" canbe used is on the initial page load, any time after the page has loaded it destroysthe page and it's contents.You can use a couple of different methods like changing the innerHTML of an elementor you can create a container for the number and append it like this:

<html><head>  <title></title><style type="text/css">span { display: block; }</style>  <script type="text/javascript">var x = 1;function mkLines(){if(x == 10) return;var oDiv = document.getElementById('d2');var nwSpan = document.createElement('span');var nwText = document.createTextNode(x++);nwSpan.appendChild(nwText);oDiv.appendChild(nwSpan);setTimeout('mkLines()', 1000);}window.onload = mkLines;</script>  </head><body><div id="d2"></div></body></html>

Thanks,

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...