Jump to content
Sign in to follow this  
jnf555

looping

Recommended Posts

hi any one help with this script i want it to go from one name to another one without stopping <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html><title>new project</title><head></head><body><script type="text/javascript">var names = ['john','gail','julie','jason','zoe'];var allnames = names.length;var i = 0;function loop(){if (i > (allnames - 1))i = 0;{document.write(names);}i++;loopTimer = setTimeout('loop()',3000);}loop();</script></body></html> thanks for any help

Share this post


Link to post
Share on other sites

document.write() is the first problem with this script. The page will never stop loading because it stops at the script and keeps trying to write.

Share this post


Link to post
Share on other sites

What you really want is for the document to have an element, like a div or something, and update its innerHTML property every time the loop iterates. The div should have an id so you can get a reference to it using document.getElementById().

Share this post


Link to post
Share on other sites

there we go this works fine <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html><title>new project</title><head></head><body><div id="jnf"></div><script type="text/javascript">var jnf2 = document.getElementById('jnf'); var names = ['john','gail','julie','jason','zoe'];var allnames = names.length;var i = 0;function loop(){if (i > (allnames - 1))i = 0;{jnf2.innerHTML = (names);}i++;loopTimer = setTimeout('loop()',3000);}loop();</script></body></html>

Share this post


Link to post
Share on other sites

<p>Here is the code</p><p> </p><div><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"></div><div><html></div><div><title>new project</title></div><div><head></div><div></head></div><div><body onload="loop()"></div><div> </div><div> </div><div><script type="text/javascript"></div><div>function loop(){</div><div>var names = ['john','gail','julie','jason','zoe'];</div><div>var allnames = names.length;</div><div> </div><div>for (var i = 0; i < names.length; i++) {</div><div> </div><div> </div><div> document.write(names+"<br>");</div><div> </div><div>}</div><div> </div><div>}</div><div></script></div><div></body></div><div> </div><div> </div><div> </div><div> </div><div></html></div><div> </div>

Share this post


Link to post
Share on other sites
<p>Here is the code</p><p> </p><div><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"></div><div><html></div><div><title>new project</title></div><div><head></div><div></head></div><div><body onload="loop()"></div><div> </div><div> </div><div><script type="text/javascript"></div><div>function loop(){</div><div>var names = ['john','gail','julie','jason','zoe'];</div><div>var allnames = names.length;</div><div> </div><div>for (var i = 0; i < names.length; i++) {</div><div> </div><div> </div><div> document.write(names+"<br>");</div><div> </div><div>}</div><div> </div><div>}</div><div></script></div><div></body></div><div> </div><div> </div><div> </div><div> </div><div></html></div><div> </div>
Why there is so many divs? its like total mess. Try adding code to codearea it might help bit. Cleared the code bit
<p>Here is the code</p><p> </p><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html><title>new project</title><head></head><body onload="loop()"><script type="text/javascript">function loop(){var names = ['john','gail','julie','jason','zoe'];var allnames = names.length;for (var i = 0; i < names.length; i++) {document.write(names[i]+"<br>");}}</script></body></html>

Edited by Mudsaf

Share this post


Link to post
Share on other sites

That code has the same problem as before. As soon as the first document.write() is called the whole page is erased.

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

×
×
  • Create New...