0

I have this code:

function myClass() {

    this.tabs = new Array();

    myClass.prototype.focus_tab = function animateTab(nr){
        for(i=0;i<this.tabs.length;i++){
            $('#' + i + '-image').stop().animate(
                { left: '100px' },
                100 , function(){
                    this.tabs[i].step = 1;
                }
            );
        }
}

but function at the end of the animation does not recognize "this.tabs". How to do it well?

Nips
  • 13,162
  • 23
  • 65
  • 103

2 Answers2

3

It's in a a different scope, try:

function myClass() {
  this.tabs = new Array();

  myClass.prototype.focus_tab = function animateTab(nr){
     for(i=0;i<this.tabs.length;i++){
         var mytab = this.tabs[i];
         $('#' + i + '-image').stop().animate({ left: '100px' }, 100 , function(){
             mytab.step = 1;
         }
      );
  }
}

There are some other issues as well, but the comments on the question already adress some of them!

adeneo
  • 312,895
  • 29
  • 395
  • 388
0

This is another example of the classic scoping issue. You only have one i variable, shared for all your callbacks. You need to make a local i for each callback. Changing you callback from:

function(){
    this.tabs[i].step = 1;
}

To:

(function(i){
    return function(){
        this.tabs[i].step = 1;
    }
})(i)
Community
  • 1
  • 1
Paul
  • 139,544
  • 27
  • 275
  • 264