Object property problem

hi all,

i’m buliding an image slider object with the following code:

  1. function imageGalery(index, width){
  2. this.isScrolling = false;
  3. this.startScroll = function(){
  4. if(this.isScrolling == false){
  5. this.isScrolling = setInterval(‘galery’+index+‘.scroll();’,30);
  6. }
  7. }
  8. this.stopScroll = function(){
  9. clearInterval(this.isScrolling);
  10. this.isScrolling = false;
  11. }
  12. img = document.createElement(‘img’);
  13. img.onmouseover = this.startScroll;
  14. img.onmouseout = this.stopScroll;
  15. }

the thing is that it start scrolling only on the second time i hover the img…

i’m guessing it’s because the stopScroll method was already launched but i don’t understand why declaring the property isn’t enough.

i have also manged to bypass it by writing “eval(‘galery’+index)” (the object’s name) instead of “this” on line 6. can someone explain?

that was quick!
thank u, storing ‘this’ will sure be helpful.
regarding the setInterval, i’ll check myself…

thanx again!

this is no longer the same inside the function. The nested function creates a new closure, and therefore creates a new sort of “environment” that has its own this. So to avoid this, you have to make a copy of the original this.

function imageGalery(index, width) {
  this.isScrolling = false;
  var gal = this;
  this.startScroll = function() {
    if (!gal.isScrolling) {
      gal.isScrolling = setInterval('galery'+index+'.scroll();',30);
    }
  }
}

The stuff inside the setInterval looks very suspicious. It looks like you’re making lots of differently named functions that do the same thing. Can’t they all be merged into a single function that takes an argument?