如果用户在过去 15 分钟内未在我们的网站上执行任何操作 activity,请从网站注销

Logout From website if user does not do any activity in our web from Last 15 minutes

如果用户在我们的网站上 15 分钟没有任何动作,如何从网站注销?

在您的 angular 应用中

npm install --save @ng-idle/core @ng-idle/keepalive angular2-moment

设置您的应用程序模块 打开 src/app/app.module.ts 并使用

导入 Ng2IdleModule
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { HttpModule } from '@angular/http';

import { NgIdleKeepaliveModule } from '@ng-idle/keepalive'; // this includes the core NgIdleModule but includes keepalive providers for easy wireup

import { MomentModule } from 'angular2-moment'; // optional, provides moment-style pipes for date formatting

import { AppComponent } from './app.component';

@NgModule({
  declarations: [
    AppComponent
  ],
  imports: [
    BrowserModule,
    FormsModule,
    HttpModule,
    MomentModule,
    NgIdleKeepaliveModule.forRoot()
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }

扩展您的主要组件

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


@Component({
  selector: 'app-theme',
  templateUrl: './theme.component.html',
  styleUrls: ['./theme.component.css']
})
export class AppComponent implements OnInit {
  lastPing?: Date = null;


  constructor(private route: Router, private idle: Idle, private keepalive: Keepalive) {
    idle.setIdle(5);
    idle.setTimeout(900);
    idle.setInterrupts(DEFAULT_INTERRUPTSOURCES);
    idle.onIdleEnd.subscribe(() => { });

    idle.onTimeout.subscribe(() => {
      this.logout();
    });
    idle.onIdleStart.subscribe(() => {
    });
    idle.onTimeoutWarning.subscribe((countdown) => {
    });

    keepalive.interval(5);

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

    this.reset();


  }

  ngOnInit() {
  }

  reset() {
    this.idle.watch();

  }

  logout() {
   //--
// LogoutCode
//---
  }
}