TypeScript setTimeout loop passing this error

前端 未结 2 1789
陌清茗
陌清茗 2021-01-04 05:21

Trying to create a timer loop in TypeScript:

timeout() {
    setTimeout(function () {
        console.log(\'Test\');
        this.timeout();
    }, 1000/60);         


        
2条回答
  •  既然无缘
    2021-01-04 05:34

    Because of this context is lost. Use the arrow function this is better.

    timeout() {
        setTimeout(() => {
            console.log('Test');
            this.timeout();
        }, 1000/60);
    }
    

提交回复
热议问题