JavaScript setTimeout Issue

So, I realized how stupid I was today when I was trying to build a slideshow with JavaScript. Well, I'm obviously going to use the setTimeout or setInterval timing functions.

Well, my JS code was quite extensive due to it being integrated with other functionality. Here is an example of the code:

// Show the thumbnails.
for (var t = 0; t < preLoad.length; t++) {
   if (document.getElementById("thumb_" + t)) {
      document.getElementById("thumb_" + t).style.border = "2px solid black";
      }
   }
document.getElementById("thumb_" + j).style.border = "2px solid blue";
   
t = setTimeout("runSlideShow('forward',false)", slideShowSpeed);

If you can see my problem, its because I used the "var t" in the loop and then used it as the object for the setTimeout. Well, there is actually a "var t;" declaration outside of this function that makes the t variable global. So, after trying to access it after the loop, it caused all sorts of problems, like multiple timeouts running at the same time on one slideshow. Whew it was confusing.

Anyway, once I changed that "var t" in the loop to say "var k", it all worked fine.

Comments
Contact Chris SchofieldBlogCFC was created by Raymond Camden. This blog is running version 5.9.001.