用户在应用程序中处于非活动状态一段时间后,Ionic 3重定向?

问题描述

我们如何检测用户在离子应用程序中处于非活动状态,以便我们可以再次将其重定向登录页面

根据韦斯利答案

// idle logout timer set to 10 second
public idlelogoutTimer: number = 10000;

// First execute the timer and wait for hostListener 
onInit() {
  this.restartIdlelogoutTimer();
}   

// Whenever users touch action occure this function will execute.
@HostListener('touchstart')
onTouchStart() {
  this.restartIdlelogoutTimer();
}

// First this will clear timeout then again setTimeout fun and counting start again,After specific time if user did not active then log out function will execute.

restartIdlelogoutTimer() {
 clearTimeout(this.idlelogoutTimer);
   this.idlelogoutTimer = setTimeout(()=>{
   this.logoutUser();
 },60000);
}

logoutUser() {
  // your logout logic here
}

解决方法

如何?

将超时设置为您认为某人“空闲”的任何时间。然后听“ touchstart”事件,并在每次用户触摸屏幕时触发超时以重新启动。

在app.component.ts

@HostListener('touchstart')
onTouchStart() {
    this.restartIdleLogoutTimer();
}

idleLogoutTimer;

OnInit() {
  restartIdleLogoutTimer();
}

restartIdleLogoutTimer() {
  clearTimeout(this.idleLogoutTimer);
  this.idleLogoutTimer = setTimeout(()=>{
    this.logoutUser();
  },60000);
}

logoutUser() {
  // your logout logic here
}