setInterval and setTimeout in Typescript
The problem in your code is there:
setInterval(this.printMsg(), 2000);
setInterval
accepts a function as a first parameter. Expression this.printMsg()
is a call of the function, and is void
actually. There are two ways to fix it. Use lambda:
setInterval(() = > this.printMsg(), 2000);
Or use bind:
setInterval(this.printMsg.bind(this), 2000);