Here is the code snippet:
import {Component, OnInit} from '@angular/core';
import {SimpleTimer} from 'ng2-simple-timer';
@Component({
'selector': 'my-app',
'template': `
<p>
ng2-simple-timer can be found on
<a href="https://www.npmjs.com/package/ng2-simple-timer">npm</a> as well as
<a href="https://github.com/J-Siu/ng2-simple-timer">github</a>.
This specific example is available at
<a href="https://github.com/J-Siu/ng2-simple-timer-example">github</a>.
</p>
<div style="border: 1px solid;margin:5px;padding:5px">
<h3>{{title}}</h3>
<div>{{counter0}}</div>
</div>`
})
export class AppComponent implements OnInit {
title = 'Example of Angular2 Simple Timer Service';
counter0 = 0;
timer0Id: string;
timer0button = 'Subscribe';
// Initializing SimpleTimer as 'st'
constructor(private st: SimpleTimer) { }
ngOnInit() {
this.st.newTimer('1sec',1);
this.subscribeTimer0();
}
subscribeTimer0() {
if (this.timer0Id) {
// Unsubscribing if timer Id is defined
this.st.unsubscribe(this.timer0Id);
this.timer0Id = undefined;
this.timer0button = 'Subscribe';
console.log('Timer 0 has been unsubscribed.');
} else {
// Subscribing if timer Id is undefined
this.timer0Id = this.st.subscribe('1sec', e => this.timer0callback());
this.timer0button = 'Unsubscribe';
console.log('Timer 0 has been subscribed.');
}
console.log(this.st.getSubscription());
}
timer0callback() {
this.counter0++;
}
}
I am currently trying to understand how timers work. I came across this piece of code online and installed ng2-simple-timer to run it. However, I encountered an error message in the following line:
this.timer0Id = this.st.subscribe('1sec', e => this.timer0callback());
The error states: "Argument of type '(e:any)=>any' is not assignable to parameter of type '()=>void.' What does this mean? Why am I receiving this error?