Click to See Complete Forum and Search --> : i can't figure out why....


Greelmo
05-20-2003, 03:43 PM
why doesn't this work!!??

<html>
<head>
<title>Hello</title>
<SCRIPT LANGUAGE="JavaScript">
var boxy=new Array("This", "is", "a", "box");
function fncbox()
{
for (i=0; i<=3; i++){dexter=setTimeout(document.formy.texty.value=boxy[i], 1000);}
}
</script>
</head>
<body onLoad="fncbox();">
<form name="formy">
<input type=text name="texty">
</form>
</body>
</html>
:confused:

Jona
05-20-2003, 03:50 PM
<html>
<head>
<title>Hello</title>
<SCRIPT LANGUAGE="JavaScript">
var boxy=new Array("This", "is", "a", "box");
function loopBox(){
for(var j=0;j<boxy.length;j++){
alert(boxy[j]);
document.formy.texty.value=boxy[j];
} }
</script>
</head>
<body>
<form name="formy">
<input type=text name="texty" onclick="setTimeout('loopBox()', 1000);">
</form>
</body>
</html>

A1ien51
05-20-2003, 03:50 PM
The reason why it does not work is you are using a for loop. It is calling all of the setTimeouts less than a millisecond of each other.

what you should do is use setInterval("TheFunction()",time)

just add a counter and then cancel the Interval when it runs out of items.

Eric

Greelmo
05-20-2003, 03:52 PM
thanks guys, i'm not to hot at arrays... now i think i understand them a bit better. Thanks again.

Jona
05-20-2003, 03:55 PM
Yes, using setInterval() would be better than using setTimeout, but in this case either works.