Angular 2-ng-idle timout,即使用户正在进行交互

时间:2017-03-30 08:16:48

标签: angular session-timeout ng-idle

我的问题是即使用户正在与应用程序交互,弹出窗口也会出现。 只有在用户空闲时才会出现弹出窗口。

会话timout.component.ts

    import { Component, OnInit } from '@angular/core';
            import { Idle, DEFAULT_INTERRUPTSOURCES} from '@ng-idle/core';
         // import { Keepalive } from '@ng-idle/Keepalive';


        @Component({
          selector: 'session-timeout',
          templateUrl: './sessionTimeout.component.html',
          styleUrls: ['./sessionTimeout.component.less']
        })

        export class SessionTimeoutComponent implements OnInit {
          idleState = 'Not started.';
          timedOut = false;
          //lastPing?: Date = null;
          showModal: boolean = false;
          sessionExt: String = "fail";
          maxTimeout = 60; //testing
          idleTime = 10; // testing
          currentTimeOut = 0;
          _model = ModelLocator.getInstance();
          private baseUrl_ssologout: string = "home.ts"
          constructor(public idle: Idle, /*private keepalive: Keepalive,*/ private sessionTimeoutService: SessionTimeoutService) {
            idle.watch();
            // sets an idle timeout of 5 seconds, for testing purposes.
            idle.setIdle(this.idleTime);

            // sets a timeout period of 5 seconds. after 10 seconds of inactivity, the user will be considered timed out.
            idle.setTimeout(this.maxTimeout);

            // sets the default interrupts, in this case, things like clicks, scrolls, touches to the document
            // idle.setInterrupts(DEFAULT_INTERRUPTSOURCES);
            idle.onTimeout.subscribe(() => {
              if (this.showModal) {
                this.logout();
              }
            });
            idle.onIdleStart.subscribe(() => {
              this.showModal = true;
            });
            idle.onTimeoutWarning.subscribe((countdown) => {
              this.idleState = 'You\'ll logged out in ' + this.getRealCountdown(countdown);
              // idle.clearInterrupts();
            });
          }
    logout() {
        //logout
      }
    stayConnected() {
        this.idle.watch();
        this.idleState = 'Started.';
        this.timedOut = false;
        this.showModal = false;
      }
   }
}

app.component.html

<session-timeout></session-timeout>

<app-header></app-header>
<router-outlet></router-outlet>
<app-amex-footer></app-amex-footer>

如果启用了idle.setInterrupts(DEFAULT_INTERRUPTSOURCES),则问题是如果弹出窗口并且用户单击页面上的任何位置,则计时器将停止。

我需要在用户理想时显示弹出窗口,并且当显示弹出窗口时,用户应该能够继续点击&#34;保持连接&#34;按钮或注销。

感谢。

1 个答案:

答案 0 :(得分:0)

这对我有用。 在进行idle.watch()之前,请保持此状态。 (取消注释您的情况)

idle.setInterrupts(DEFAULT_INTERRUPTSOURCES);

然后在打开模式添加之前,在idle.onIdleStart.subscribe()方法内:

idle.clearInterrupts();

最后,在modalRef.result.then()中,如果结果为“ continue”,则将中断设置回默认值。

this.idle.setInterrupts(DEFAULT_INTERRUPTSOURCES);