setInterval()によって呼び出されるtimed関数の内部から関数を呼び出そうとしています。ここでTS TypeScript SetInterval - タイマターゲット関数の内部から関数にアクセスできない
は私のコードです:
export class SmileyDirective
{
FillGraphValues()
{
console.log("FillGraphValues works"); // this works
}
MyTimer()
{
console.log("timer works"); // this works
this.FillGraphValues(); // this does not work
FillGraphValues(); // this does not work
}
draw()
{
this.FillGraphValues(); // this works
setInterval(this.MyTimer, 1000);
}
}
アプリケーションのいずれかでクラッシュ:
"this.FillGraphValues is not a function"
または
Cannot find name 'FillGraphValues'. Did you mean the instance member 'this.FillGraphValues'?
私も試してみました:
setInterval(function(){MyTimer()}, 1000);
と
setInterval(function(){this.MyTimer()}, 1000);
しかし、彼らは動作しませんでした。
感謝:)
'this.FillGraphValues(); //これは動作しません.' setIntervalコールバック内のMyTimer()の呼び出しを意味しますか? –
試してみる 'this.myTimer.bind(this)' 'setTimeout/setInterval'は指定された時刻にイベントを登録するだけなので、コンテキストが失われる – Rajesh
[コールバック内の正しい\' this \ 'にアクセスする方法] (http://stackoverflow.com/questions/20279484/how-to-access-the-correct-this-inside-a-callback) – Rajesh