timeout

TypeScript setTimeout loop passing this error

百般思念 提交于 2020-11-27 04:57:11
问题 Trying to create a timer loop in TypeScript: timeout() { setTimeout(function () { console.log('Test'); this.timeout(); }, 1000/60); } But after the first loop works correctly I'm getting this error: "Uncaught TypeError: this.timeout is not a function". It seems that the this variable does not exist after the initial loop. Any ideas? 回答1: Because your this doesn't refer to the object. Every function has it's own this. So your this is the one which is defined by anonymous function inside the

TypeScript setTimeout loop passing this error

↘锁芯ラ 提交于 2020-11-27 04:55:44
问题 Trying to create a timer loop in TypeScript: timeout() { setTimeout(function () { console.log('Test'); this.timeout(); }, 1000/60); } But after the first loop works correctly I'm getting this error: "Uncaught TypeError: this.timeout is not a function". It seems that the this variable does not exist after the initial loop. Any ideas? 回答1: Because your this doesn't refer to the object. Every function has it's own this. So your this is the one which is defined by anonymous function inside the