Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- private _timeoutSeconds: number = 15;
- private timerSubscription: Subscription;
- private timer: Observable<number>;
- private resetOnTrigger: boolean = false;
- public timeoutExpired: Subject<number> = new Subject<number>();
- constructor(
- public router: Router,
- private cd: ChangeDetectorRef) {
- this.timeoutExpired.subscribe(n => {
- alert("logout");
- });
- this.startTimer();
- }
- public startTimer() {
- if (this.timerSubscription) {
- this.timerSubscription.unsubscribe();
- }
- this.timer = Observable.timer(this._timeoutSeconds * 1000);
- this.timerSubscription = this.timer.subscribe(n => {
- this.timerComplete(n);
- });
- }
- public stopTimer() {
- console.log(this.cd + " " + "stop timer");
- this.timerSubscription.unsubscribe();
- }
- public resetTimer() {
- console.log(this.cd + " " + "reset timer");
- if (this.timerSubscription) {
- this.timerSubscription.unsubscribe();
- }
- this.timer = Observable.timer(this._timeoutSeconds * 1000);
- this.timerSubscription = this.timer.subscribe(n => {
- this.timerComplete(n);
- });
- }
- private timerComplete(n: number) {
- console.log(this.cd.detectChanges() + " " + "timer complete");
- this.timeoutExpired.next(++this._count);
- this.resetTimer();
- if (this.resetOnTrigger) {
- this.startTimer();
- }
- }
Add Comment
Please, Sign In to add comment