View Full Version : Ultimate Fade-in slideshow: Code Question

06-08-2007, 10:39 PM
1) Script Title: Ultimate Fade-in slideshow (v1.5)

2) Script URL (on DD): http://www.dynamicdrive.com/dynamicindex14/fadeinslideshow.htm

3) Describe problem:

I have read through the code and all of it makes sense, except I am stuck on one part. Could someone please explain the logic behind the cacheobj in fadeshow.prototype.rotateimage:

// If pause onMouseover enabled, cache object
if(this.pausecheck == 1)
var cacheobj = this;

if(this.mouseovercheck == 1)
setTimeout(function(){cacheobj.rotateimage()}, 100);

Specifically, why create a new var cacheobj rather than simply use 'this' in setTimeout()?

06-08-2007, 11:26 PM
okay. I think I know. It's a closure. I forgot about the context-dependent nature of 'this.' Thus, cachobj provides a concrete reference to the fadeshow instance, where 'this' does not.

06-09-2007, 02:33 AM
You hit the nail right on the head.