Home > database >  TypeScript/Javascript - TypeError this.function is not a function
TypeScript/Javascript - TypeError this.function is not a function

Time:10-07

I'm a bit new to TypeScript but know JavaScript from before. I have my code for a wheel mini-game for my app and I get the following error on my console.

this.easeOut is not a function

The code is the following (only what is important to the problem I think):

spin() {
  const spinAngleStart = Math.random() * 10   10;
  this.spinTime = 0;
  this.spinTimeTotal = Math.random() * 3   4 * 1000;
  this.rotateWheel(spinAngleStart);
}

rotateWheel(spinAngleStart: number){
  this.spinTime  = 30;
  if(this.spinTime >=  this.spinTimeTotal) {
    this.stopRotateWheel();
    return;
  }
  *const spinAngle = spinAngleStart - this.easeOut(this.spinTime, 0, spinAngleStart, this.spinTimeTotal);*
  this.startAngle  = (spinAngle * Math.PI / 180);
  this.drawRouletteWheel();
  console.log(this.spinTimeTotal);
  console.log(this.spinTime);
  this.spinTimeout = setTimeout(this.rotateWheel, 30);
 } 

easeOut(t, b, c, d) {
   const ts = (t /= d) * t;
   const tc = ts * t;
   console.log(ts);
   console.log(tc);
   return (b c * (tc   -3*ts   3*t));
  }

As you can see easeOut() does exists inside the same hierarchy of my rotateWheel() function and everything else if not sending any errors, so I do not why it sends an error.

the error is sent to the line with the asterisks

CodePudding user response:

You either want setTimeout(() => this.rotateWheel(), 30) or setTimeout(this.rotateWheel.bind(this), 30);. Otherwise, when rotateWheel is called, this is bound to the window object (or global object in node).

  • Related