一段时间后自动注销,angular6中只有用户未检测到

时间:2018-12-14 06:10:17

标签: angular typescript session timeout angular-changedetection

要求:

  1. 在特定时间(5分钟或10分钟)后自动注销应用程序。
  2. 注销期间需要检查我们的应用程序中反映的任何更改检测。
  3. 如果是,则无需注销该应用程序,否则我们将注销该应用程序

我有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();
    }
}

0 个答案:

没有答案