Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Letters 1 at a time

Status
Not open for further replies.

mpalmer12345

Programmer
Feb 16, 2004
59
US
I am trying to print out the letters of MYtext one at a time at 1 second intervals. This doesn't work, all I get is the first letter, then it stops. My grasp of setTimeout is very shaky, it seems.

<HTML>
<HEAD>
<script language="JavaScript">
<!--

MYtext = "black";

function changeTX(var ii) {
MYlen = MYtext.length;
MYsub = MYtext.substring(ii, ii + 1);
document.write(MYsub + " " + xx + "<BR>");
}

var ii = 0;

function newTX(){
while (ii < 10) {
var newtext = setTimeout("changeTX(ii)", 1000);
ii += 1;
}
}

//-->
</script>

</HEAD>
<BODY BGCOLOR=#FFFFFF onLoad="newTX()">
</BODY>
</HTML>

 
Well... your code will trigger 10 setTimeOuts in quick succession... and in the rush to run them all... you are getting only one result back. Besides - there were some other errors in the code you pasted that may have broken things too.

This sample page shows an alert (instead of a document.write) that will alert each of the letters contained in the string MYtext regardless of how long that string is. It will alert one letter every second.

Code:
<html>
<head>
<script type="text/javascript">
MYtext = "black";

function changeTX()
{
  MYlen = MYtext.length;
  MYsub = MYtext.substring(ii-1, ii);
  alert(MYsub + " " + ii + "<BR>");
  newTX();
}

var ii = 0;
function newTX()
{
  if (ii < MYtext.length)
  {
    var newtext = setTimeout("changeTX(ii)", 1000);
    ii++;
  }
}
</script>
</head>
<body onload="newTX()">
</body>
</html>

Jeff
 
Thanks for the help! I can't get this code of yours to run. I fixed a small error, and still nothing,

var newtext = setTimeout("changeTX(ii)", 1000);

to

var newtext = setTimeout("changeTX()", 1000);
 

Surely the line should actually read:

Code:
var newtext = setTimeout("changeTX(" + ii + ")", 1000);

?

Dan
 
Dan...

Not in this instance... since he's accessing the variable (globally) each iteration.

Jeff
 
Using document.write also destroys previous content,
so if you want to see the whole string you can change
the "innerHTML" of a div like(using jeff's start):

Code:
<html><head>
<script type="text/javascript">
MYtext = "black";
MYtmp = "";
function changeTX()
{
  el = document.getElementById('out');
  MYlen = MYtext.length;
  MYsub = MYtext.substring(ii-1, ii);
  MYtmp += MYsub;
  el.innerHTML = MYtmp;
  newTX();
}

var ii = 0;
function newTX()
{
  if (ii < MYtext.length)
  {
    var newtext = setTimeout("changeTX(ii)", 1000);
    ii++;
  }
}
</script></head>
<body onload="newTX()">
<div id="out">
</div>
</body></html>

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top