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

Ionic 3 redirect after user In-active in app for some time?

我们如何检测用户在 ionic 应用程序中处于非活动状态,以便我们可以再次在登录页面中重定向 him/her。

根据卫斯理的回答

// 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
}