2

我使用了 Ng Idle 版本 6.0.0-beta.3 并使用 angular 6,这个库工作正常,系统开启,但是当系统进入睡眠模式时计时器停止。如何实现空闲状态是系统进入睡眠状态?

    import {Idle, DEFAULT_INTERRUPTSOURCES} from '@ng-idle/core';
    import {Keepalive} from '@ng-idle/keepalive';
    constructor(
    private idle: Idle, private keepalive: Keepalive) {

    // sets an idle timeout of 5 seconds, for testing purposes.
    idle.setIdle(900);
    // sets a timeout period of 5 seconds. after 10 seconds of 
    inactivity, the user will be considered timed out.
    idle.setTimeout(5);
    // sets the default interrupts, in this case, things like clicks, 
    scrolls, touches to the document
    idle.setInterrupts(DEFAULT_INTERRUPTSOURCES);

    idle.onIdleEnd.subscribe(() => {this.idleState = 'No longer idle.'
      console.log(this.idleState)
    });
    idle.onTimeout.subscribe(() => {
    if(sessionStorage.getItem('loggedInTab') == 'true' || 
    localStorage.getItem('loggedInStatus') == 'true'){
    this.logoutService.logout().subscribe(res => {
    console.log(res);
    localStorage.clear();
    this.router.navigate(['/']);
    });
    }
    this.idleState = 'Timed out!';
    this.timedOut = true;
    });
    idle.onIdleStart.subscribe(() => {this.idleState = 'You\'ve gone 
    idle!';
    console.log(this.idleState);
    });
    idle.onTimeoutWarning.subscribe((countdown) => this.idleState = 'You 
    will time out in ' + countdown + ' seconds!');

    // sets the ping interval to 15 seconds
    keepalive.interval(15);

    keepalive.onPing.subscribe(() => this.lastPing = new Date());

    this.reset();
    }

   reset() {
   this.idle.watch();
   this.idleState = 'Started.';
   this.timedOut = false;
   }

   ngOnDestroy() {
   this.idle.stop();
   }

我使用了 Ng Idle 版本 6.0.0-beta.3 并使用 angular 6,这个库工作正常,系统开启,但是当系统进入睡眠模式时计时器停止。如何实现空闲状态是系统进入睡眠状态?

4

1 回答 1

0

如果您在观看之前将Timeout 设置为false,那么在Windows 上它将在计算机从睡眠状态恢复后立即注销用户。不适合我在 Mac 上工作。

reset() {
   this.idle.setTimeout(false); //add this line
   this.idle.watch();
   this.idleState = 'Started.';
   this.timedOut = false;
}

https://github.com/moribvndvs/ng2-idle/issues/112

于 2021-02-11T08:08:36.707 回答