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 Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

problems with setTimeout()

Status
Not open for further replies.

secretsquirrel

Programmer
Mar 22, 2001
202
GB
hi,

i've got a div, which contains a bit of text. i would like the page to load, wait a few seconds and change the div to a different bit of text, wait a few seconds more and change back to the original text, wait and then repeat.

i've tried the following, but it seems to crash the browser!

Code:
var lower = "blah blah blah";
var upper = "BLAH BLAH BLAH";

function test()
{
	for(var i = 1; i > 0; i)
	{
		if(myDiv.innerHTML == "blah blah blah")
		{
			setTimeout("myDiv.innerHTML = upper", 3000);
		} else {
			setTimeout("myDiv.innerHTML = lower", 3000);
		}
	}
}

...can anyone suggest a way to do this? any help is appreciated.

thanks,

ss...
 
Maybe this:
Code:
for(var i = 1; i > 0; i[COLOR=red]--[/color])
 
at the moment this swaps between 2 bits of content when i click on the image, but ultimately i want this to automatically swap between 2 bits of content.

can anyone suggest the easiest way to do this?

the code is...

Code:
var lower = "blah blah blah";
var upper = "BLAH BLAH BLAH";

function test()
{
	for(var i = 1; i > 0; i--)
	{
		if(myDiv.innerHTML == "blah blah blah")
		{
			setTimeout("myDiv.innerHTML = upper", 1000);
		} else {
			setTimeout("myDiv.innerHTML = lower", 1000);
		}
	}
}

...and...

Code:
<div id="myDiv" style="border: 1px solid #000000; width: 300px; height: 300px;">
BLAH BLAH BLAH
</div>
<img src="" width="50" height="40" border="1" onClick="test();">

thanks,

ss...
 
Basically you could use
Code:
<body onload="test();">
... but this code has some goofs and flaws. First, there is no reason for using for() loop. Second, comparing innerHTML is not a good idea - the same string appears twice in code, plus browser may normalize innerHTML. Third, direct addressing of DOM element (myDiv) works only in IE.

Try something like:
Code:
<script language="javascript">
var aSlides = [ "blah blah blah", "BLAH BLAH BLAH", "BlAh BlAh BlAh" ];
var iSlide = 0;
var bLoop = false;
var iSpeed = 1000

function test()
{	var oDiv = document.getElementById("myDiv");

	oDiv.innerHTML = aSlides[iSlide];
	iSlide = (iSlide+1) % aSlides.length;
	
	if (bLoop || iSlide!=0) setTimeout( "test()", iSpeed );
}
</script>

<body onload="test();">
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top