title: Passing a callback to method of an Object
body: <p>I have an Object constructor who manage a lot of animations I have on a website, i will like to pass a parameter to the start function, this parameter is a callback, like a oncomplete is what I want to achieve because in some cases when the animation stop I want some elements to appear. The problem is i pass the callback but nothing happens and I really dont know why the function is not taking the parameters, I leave you some code I hope someone can help me.</p>
  
  <pre><code>// CONSTRUCTOR WHO MANAGE THE ANIMATIONS FOR THE WEBSITE
          function SpriteAnimation(frameWidth, spriteWidth, spriteElement, shouldLoop, frameRate){
              this.frameWidth = frameWidth;
              this.spriteWidth = spriteWidth;
              this.selector = document.getElementById(spriteElement);
              this.shouldLoop = shouldLoop ;
              this.curPx = 0;
              this.frameRate = frameRate;
          }
  
          SpriteAnimation.prototype.start = function(callback){
  
              this.selector.style.backgroundPosition = "-" + this.curPx + "px 0px";
              this.curPx += this.frameWidth;
  
              if (this.curPx &lt; (this.spriteWidth - this.frameWidth)){
                  setTimeout(this.start.bind(this), this.frameRate);
              } else if (this.shouldLoop) {
                  this.curPx = 0;
                  this.start();
  
                  if(callback &amp;&amp; typeof callback === "function"){
                      callback();
                  }
              }
  
          }; 
  </code></pre>
  
  <p>Now the pice of code I am using to call the callback:</p>
  
  <pre><code>var letter = new SpriteAnimation(789.695652, 18163, "letter", false, 53.3);
  
  letter.start(function(){
      $("#form-field").fadeIn();
  });
  </code></pre>
  
  <p>I don't know if it's probably that I am not passing the context or something like that but actually I am learning javascript so don't understand really well how it works with objects. Before I just had a bunch of variables and functions per animation.</p>