Script not working in either FF or IE

There still seem to be some problems with the code though, such as the array needing to be of strings, and counting from 1 being inappropriate for arrays, it should count from 0 instead.

The timeout function cannot access the step/images/numOfImages variables either, so how do you want to deal with that?

Do you want them to be global variables, to be passed to the function, or to store them somewhere that the function can have access to?
I’ll pass them to the function in this case.

If we start the function with only the images, we can use a simple sanitization of the step variable to ensure that an undefined value for step becomes a number too.

And just to be picky, we’ll run that code through jslint.com (in “The Good Parts” mode) to easily pick up any remaining issues.

So, how’s this (it works now) as an improvement on the code?


function slideit(images, step) {
    if (!document.images) {
        return;
    }
 
    step = step || 0;
    document.images.slide.src = images[step] + '.gif';
 
    step = (step + 1) % images.length;
    setTimeout(function () {
        slideit(images, step);
    }, 2500);
}

var images = ['firstcar', 'secondcar', 'thirdcar'];
slideit(images);

Paul, slack of me as I should have tested it.

The timeout function cannot access the step/images/numOfImages variables either, so how do you want to deal with that?

It can can’t it?:slight_smile:

I haven’t got the images, but if you swap ‘document.images.slide.src =’ for console.log(images[step] + ‘.gif’) it’s outputs the filenames just fine.

var step = 0,
    images = new Array( 'firstcar', 'secondcar', 'thirdcar' ),
    numOfImages = images.length-1;
      
function slideit(){
  if (!document.images) { return; }
 
  document.images.slide.src = (images[step] + '.gif');
 
  if (step < numOfImages) {
    step++;
  } else {
    step = 0;
  }
  setTimeout(slideit, 2500);
}
 
slideit();

Just to test with console.log

(function(){

var step = 0,
    images = new Array( 'firstcar', 'secondcar', 'thirdcar' ),
    numOfImages = images.length-1;
      
function slideit(){
  if (!document.images) { return; }
 
  console.log(images[step] + '.gif');
 
  if (step < numOfImages) {
    step++;
  } else {
    step = 0;
  }
  setTimeout(slideit, 2500);
}

slideit();
}());

That’s odd, I must have misunderstood another error in the previous code to be that issue.

Still, we now have viable working code for that tutorial from the year 2002.
if you’re interested, there’s a timeline from archive.org of changes to that tutorial page.

Still, we now have viable working code for that tutorial from the year 2002.
I hadn’t checked the year. There were a few clues there though.