The problem is that when you pass the strings that should be evaluated by calling "setTimeout", the evaluation will be performed (later, when there is time to start) in the global context. This way you are much better (for many other reasons) passing in real functions:
setTimeout(function() { delay(images, 0); }, 2000); function delay(arr, num) { document.slide.src = arr[num % 3]; setTimeout(function() { delay(arr, num + 1); }, 1000); }
In more modern browsers, you can use the ".bind ()" method to create functions that are pre-bound to something that will be used like this :
setTimeout(delay.bind({arr: images, num: 0}), 2000); function delay() { document.slide.src = this.arr[this.num % 3]; setTimeout(delay.bind({arr: this.arr, num: this.num + 1}), 1000); }
Six out of one, half a dozen another, but as an example that shows that there are several ways to do something.
Pointy
source share