要求:
我有google,但是即使我也正在更改应用程序,也要注销。
从{导入{可观察,主题,订阅,BehaviorSubject} 'rxjs / rx';
@Component({ 选择器:“ app-root”, templateUrl:“ ./ app.component.html”, changeDetection:ChangeDetectionStrategy.OnPush})
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(); } }