Scripting: Need Help with looping arrays

started by Ratznjammers on Apr 27, 2005 — RSS Feed

Ratznjammers Ratznjammers
Posts: 4

The following code loops one time ....
I would like it to loop indefinitely ...
Can anyone help?

<head>
<title>SurfBoard - Riding the Internet wave!</title>

<script language="JavaScript"><!--
 var site = 0;
 var delay = 15;
 var num_sites = 9;
 var counter = delay;

 function loadSites() {
   sitename = new Array(num_sites);
   sitename[0]=' Image 0';
   sitename[1]=' Image 1';
   sitename[2]=' Image 2';
   sitename[3]=' Image 3';
   sitename[4]=' Image 4';
   sitename[5]=' Image 5';
   sitename[6]=' Image 6';
   sitename[7]=' Image 7';
   sitename[8]=' Image 8';
 
   siteurl = new Array(num_sites);
   siteurl[0]='http://xxxxxxxxxx.com/ban/Image0.html';
   siteurl[1]='http://xxxxxxxxxx.com/ban/Image10.html';
   siteurl[2]='http://xxxxxxxxxx.com/ban/Image11.html';
   siteurl[3]='http://xxxxxxxxxx.com/ban/Image12.html';
   siteurl[4]='http://xxxxxxxxxx.com/ban/Image13.html';
   siteurl[5]='http://xxxxxxxxxx.com/ban/Image14.html';
   siteurl[6]='http://xxxxxxxxxx.com/ban/Image15.html';
   siteurl[7]='http://xxxxxxxxxx.com/ban/Image18.html';
   siteurl[8]='http://xxxxxxxxxx.com/ban/Image20.html';

 }

 function countDown() {
   if ((document.surfboard.autosurf.checked) & (site != (num_sites - 1))) {
     document.surfboard.timer.value = counter-- + ' sec';
     if (counter != -1)
       setTimeout('countDown()',1000);
     else {
       forward();
       countDown();
     }
   }
 }

 function displayDelay() {
   document.surfboard.timedelay.value = delay;
 }

 function displayInfo() {
   document.surfboard.sitenamefield.value = sitename[site];
   document.surfboard.siteurlfield.value = siteurl[site];
 }

 function back() {
   if ( site == 0 )
     alert('Already at first site!')
   else {
     site--;
     counter = delay;
     load(siteurl[site]);
     displayInfo();
   }
 }

 function forward() {
   if ( site == (num_sites - 1) )
     alert('Last site!')
   else {
     site++;
     counter = delay;
     load(siteurl[site]);
     displayInfo();
   }
 }

 function load(url) {
   opener.location.href= url;
 }

 function makeTop() {
   if (document.surfboard.ontop.checked)
     self.focus();
 }

//--></script>
</head>

Thank You

Joe Gillespie Joe Gillespie
Posts: 528

The way to do this, instead of firing off timers all over the place is simply to read the Date object and use getTime() in a recursive loop.

Check this out...

http://www.wpdfd.com/editorial/timer.html

for an example of a recursive loop - refreshClock().

With a little math, you can easily change the image based on the current second or millisecond - so, if the seconds divides exactly by 15 (use modulo operator %), for instance, you move to the next slide. Keep an incrementing index count of the slide number and

if(index == 9) {index = 0;}

Use the index as the array dimension...

sitename[index]

Ratznjammers Ratznjammers
Posts: 4

Thank you for the input, however I am unable to understand what you are talking about as I am very new to Javascript and I keep going around in circles.

If possible can you just tell me where and what to add to the code? Or better yet just place the correction into the code.

Thanks
Wayne

Joe Gillespie Joe Gillespie
Posts: 528

If is was a simple as that, I would, but your approach is all wrong. If you use the timer method more than a few times in succession, all those timer calls fill up memory (the stack) very quickly and the browser will crash - maybe the whole system.

I think that you need to find a script specifically for the task or get someone to write one for you.

David David
Posts: 40

If I recall correctly, Firefox doesn't allow recurrsive looping. At least, the option is available but switched off by default. Something to bear in mind.

Ratznjammers Ratznjammers
Posts: 4






 function forward() {
   if ( site == (num_sites - 1) ) [[ change this to ( site == 9 ) ????
alert('Last site!') [[ change this alert to a call to site=0 ]]  ????
   else {
site++;
counter = delay;
load(siteurl[site]);
displayInfo();
   }

It seems like all one needs to do is
change this function from an alert to
a call to site=0

As this code operates in Frames there
is no 'crash' ,,, as I have run the code
480 image views and ram memory usage
has not changed. and I doubt that the
stack has been overloaded, as no
crash has occurred.

why would you have me re-write the whole
code? when only a minor change is necessary

David David
Posts: 40

Here's some pseudo code:
 
for(;{  //infinite loop
 for (i=0;i<10;i++){  // counts up from 1 to 9
   load i.html;
   refresh_clock();
 }
}


>> why would you have me re-write the whole  
code? when only a minor change is necessary


Your code is about 10 times the size of what it needs to be. Why make the computer work harder than it has to? Besides which, the less code you have, the less room there is for bugs.

You must login to reply