1

i've been looking for an answer but everything i find seems to be saying that i'm not doing anything wrong. I'm trying to call a prototype function inside another one, but i keep getting the error that it is not a function.

For some reason it only happens within one specific method (tick), the rest has no problem with the same methods.

Here is my code:

Timer.prototype.getTimeRemaining = function(){
  return this.startTime - this.currentTimeTimed;
}

Timer.prototype.updateElement = function(){
  document.getElementById("clock").innerHTML = this.getTimeRemaining();
  return true;
}

Timer.prototype.tick = function(){
  if (this.running){
    this.currentTimeTimed += timePassedInMilliseconds;
  }
  this.getTimeRemaining();
  this.updateElement();
}

So the error i get is in tick(): Uncaught TypeError: this.getTimeRemaining() is not a function.

While testing some more, i think i found out why: i think it is because the tick function is called from an interval. I'm not sure, but that's the only thing i can think of that's different from the other functions. But i don't know how i should change my code then, if it is caused by the interval.

Thanks in advance.

Midasso
  • 37
  • 5
  • 1
    Maybe you have some bindind issues... please, show us how is your `setInterval`. – Julio Betta Jan 10 '16 at 01:07
  • Put a breakpoint on the `this.getTimeRemaining()` line inside `tick`. When it stops there, examine the value of `this`. Proceed from there. –  Jan 10 '16 at 06:04

1 Answers1

1

I found out that i need to use .bind(this) in my setInterval.

Midasso
  • 37
  • 5
  • You may consider using call or apply: http://stackoverflow.com/questions/1986896/what-is-the-difference-between-call-and-apply – ovgu12 Jan 10 '16 at 01:17