2

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.

2
  • 1
    Maybe you have some bindind issues... please, show us how is your setInterval. Commented Jan 10, 2016 at 1:07
  • Put a breakpoint on the this.getTimeRemaining() line inside tick. When it stops there, examine the value of this. Proceed from there. Commented Jan 10, 2016 at 6:04

1 Answer 1

2

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

Sign up to request clarification or add additional context in comments.

1 Comment

You may consider using call or apply: stackoverflow.com/questions/1986896/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.