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

Getting setTimeout() to work.

Status
Not open for further replies.

gns100

Programmer
Aug 11, 2003
40
US
I want to delay the execution of nextPart() by 3 sec. each time i increments in the following code. What happens is that i increments and I only get one call to nextPart().

What's wrong?
Thanks.

Code:
<html>
<head>
<script language=&quot;JavaScript&quot; type=&quot;text/JavaScript&quot;>

var  i = 0;

</script>
</head>
<body>
</body>

<script language=&quot;JavaScript&quot; type=&quot;text/JavaScript&quot;>

	while(i<4) {
		i++;
		alert(&quot;in while loop&quot;);

// I want to suspend the program until the 3 sec has passed.
		setTimeout(&quot;nextPart()&quot;, 3*1000);
	}

	function nextPart() {

		document.write(&quot;i  = &quot; + i );
		alert(&quot;nextPart reached&quot;);
	}
</script>
</html>
 
I think I can see what you're trying to do but I have a question: do you want to wait 3 seconds and then write the different parts immediately one after the other? The way you're doing that there won't be a three second delay between each part. They will all wait three seconds but, because they were all called at practically the same time, you'll get a three second pause at the start and then all four parts will appear one right after the other.

Assuming that is what you're after, here's some code that will do that:
Code:
<script language=&quot;JavaScript&quot; type=&quot;text/JavaScript&quot;>

var i = 0;
var timeArray = new Array();
var result_string = &quot;&quot;;

    while( i < 4 ) {
        i++;
        alert( &quot;In while - i = &quot; + i );

       timeArray[i] = setTimeout( &quot;nextPart( &quot; + i + &quot; )&quot;, 3*1000 );
    }

    function nextPart( cnt ) {

        result_string += &quot;i = &quot; + cnt;
        document.body.innerHTML =  result_string;
    }
</script>

If you're after a three second pause between each part then you need to change the timeOut line a bit:
Code:
       timeArray[i] = setTimeout( &quot;nextPart( &quot; + i + &quot; )&quot;, i * 3000 );
.
 
What you recommend addresses part of my problem, but let me be more specific about what I am trying to achieve.

I have two groups of 10 images in each (A & B), (the number can change, however). The images are shown side by side - group A on the left, group B on the right in sequence 1 thru 10, after which the sequence repeats on-and-on. At the moment, the images fade (using FILTER:progid:DXImageTransform.Microsoft.Fade(Duration=2, Overlap=1.0)) in and out (at the same time) on a 6 sec interval (using setInterval()). This works OK.

However, I want to modify this so that group B images are delayed 3 sec with respect to the group A images. Both sets still change at a 6 sec interval. How do I do this? At the moment I use just one setInterval() to time the changing of both groups.

When I posed my original question I wasn't clear. I think using an array will limit the number of times the change can be performed, which I don't want to do.

Thanks

 
Okay, so you need two setIntervals of six seconds that are three seconds apart. The above code should still give you some idea of how to achieve that.

Have a crack at it. :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top