Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save automactic/851fabd122e58fdcf550306d81338aa3 to your computer and use it in GitHub Desktop.
Save automactic/851fabd122e58fdcf550306d81338aa3 to your computer and use it in GitHub Desktop.
angular2 timer
import { Component, OnInit, OnDestroy } from '@angular/core';
import { Observable, Subscription } from 'rxjs/Rx';
import { Task } from '../../model/task';
@Component({
selector: 'tasks',
templateUrl: './list-tasks.component.html',
styleUrls: ['./list-tasks.component.css']
})
export class ListTasksComponent implements OnInit, OnDestroy {
tasks: Task[] = []
private ticks = 0;
private timer: Observable<number>;
private sub: Subscription;
ngOnInit(): void {
this.timer = Observable.timer(2000,5000);
this.sub = this.timer.subscribe(t => this.tickerFunc(t));
}
tickerFunc(tick: number){
console.log(this);
this.ticks = tick
}
ngOnDestroy(){
console.log("Destroy timer");
this.sub.unsubscribe();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment