Is there a case where setInterval() or interval() is better? Or do they function the exact same? In an online/video course I am taking to increase my knowledge in Angular the guy said to use setInterval for one part, however, I couldn't figure out the syntax to use so I ended up using interval() with .subscribe() and .unsubscribe instead (code below).
It functioned exactly how I wanted to, however I still cannot find if using interval with .subscribe is better or worse. I saw that using .setInterval() in some cases is detrimental but couldn't find much of anything on interval(). If someone could give me some insight into the differences (if any) that would be great!
export class GameControlComponent implements OnInit {
@Output() numbers = new EventEmitter<{number : number}>();
//not sure what type to declare here
num;
subscription : Subscription;
constructor() { }
ngOnInit(): void {
}
//event (holding a incrementing number) should get emitted each second (ref = setInterval())
startCount() {
this.num = interval(1000);
this.subscription = this.num.subscribe(n =>
this.numbers.emit(n));
console.log(this.num);
}
stopCount() {
this.subscription.unsubscribe();
}
}