Here’s my problem:
var slide;
$$('#slides li').each(function(i, n) {
slide = i;
setTimeout("initiateSlide()",n * 500)
});
function initiateSlide(){
i = slide;
alert(i); // pretty much alerts the last one 5 times
}
I expect to initiateSlide() with 5 different slides, instead I only get the last one 5 times.
Your problem is that the global variable (slide) that you are referencing in the initiateSlide() function is set to the last one by the time the function runs. You probably want to use closures to maintain the state of the variable. Like this:
Note – This also removes the need for the global entirely