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

calling a given function at a certain interval 1

Status
Not open for further replies.

keak

Programmer
Joined
Sep 12, 2005
Messages
247
Location
CA
Hi there,

I am trying to get an iframe to load different local pages at a given interval.
I have something like
Code:
<script language="javascript">
    function rotate() {
        var urls= new Array (2);
        urls[0]="page0.html";
        urls[1]="page1.html";
        urls[2]="page2.html";

        document.getElementById('displayFrame').src = URLS[0];


    }
</script>

Is there like a timer sort of function that I can use to make this iframe loop through this array and display the html pages one by one (looping back to the first element after reaching the end)?
 
In fact, there is. It's called "setTimeout(...)". It takes two parameters. The first is a String that tells the function WHAT to do. The second is a number that tells it HOW LONG TO WAIT (in milliseconds) to do it.

For example:

Code:
function cycleThrough()
{
 for(var i=0; i<urls.length; i++)
 {
  setTimeout("document.getElementById('displayFrame').src = urls["+i+"];", 1000*i);
 }
}

i first param second param
0 urls[0] 0
1 urls[1] 1000
2 urls[2] 2000

Now, if you want the process to be forever continuous:

Code:
var iteration = 0;
function cycleThrough()
{
 document.getElementById('displayFrame').src = urls[iteration];
 iteration++;
 if(iteration == urls.length)
  iteration = 0;
 setTimeout("cycleThrough();", 1000);
}

This calls itself, every second (1000 milliseconds), and does the src-setting with each successive index of the array.

Does this make sense?

I hope that helps.

Dave


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
O Time, Strength, Cash, and Patience! [infinity]
 
Wow Dave,
That makes a whole lot of sense !!!
Thank you for the detailed post, you rock!!!
 
You're very welcome! 'glad to be of service.

Let us know if you have questions when you're implementing the cycle-through technique with your code.

Dave


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
O Time, Strength, Cash, and Patience! [infinity]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top