使用Cordova和Ionic的Typescript中的不活动检查器?
我见过npm软件包ng idle,但它不适用于爱奥尼亚移动应用程序。我已经找到了这个JavaScript代码,并对其进行了一些修改,以适合我的项目,但我得到的错误是this.router未定义使用Cordova和Ionic的Typescript中的不活动检查器?,typescript,cordova,ionic-framework,Typescript,Cordova,Ionic Framework,我见过npm软件包ng idle,但它不适用于爱奥尼亚移动应用程序。我已经找到了这个JavaScript代码,并对其进行了一些修改,以适合我的项目,但我得到的错误是this.router未定义 var timeoutID : any; constructor(public router: Router ){ //Checks for inactivity constantly this.setupInactivityChecker(); } startTimer() {
var timeoutID : any;
constructor(public router: Router ){
//Checks for inactivity constantly
this.setupInactivityChecker();
}
startTimer() {
timeoutID = window.setTimeout(this.goInactive, 5000);
}
resetTimer() {
window.clearTimeout(timeoutID);
this.goActive();
}
goInactive() {
// do something
console.log('Ahhhh.. finally inactive again. Now I may rest -_-');
this.router.navigate(['login-screen']);
}
goActive() {
// do something
console.log('Active again');
this.startTimer();
}
setupInactivityChecker() {
addEventListener("mousemove", this.resetTimer(), true);
addEventListener("mousedown", this.resetTimer(), true);
addEventListener("keypress", this.resetTimer(), true);
addEventListener("DOMMouseScroll", this.resetTimer(), true);
addEventListener("mousewheel", this.resetTimer(), true);
addEventListener("touchmove", this.resetTimer(), true);
addEventListener("MSPointerMove", this.resetTimer(), true);
this.startTimer();
}
还有一个附带说明:有人能解释为什么我不能在处理程序中使用括号吗?例如:
timeoutID = window.setTimeout(this.goInactive, 5000);
我只是想让应用程序在空闲一段时间后注销,这样任何链接都会很感激
好了,伙计们,这是一个很好用的版本
let outsideRouter : any;
var timeoutID : any;
export class ScannerLogicService {
constructor(private network: Network, private router: Router,public barcode: BarcodeScanner, public alertController: AlertController, private http: HttpClient, private dialog: Dialogs) {
outsideRouter = router;
setupInactivityChecker();
}
}
function startTimer() {
timeoutID = window.setTimeout(goInactive, 1800000);
}
function resetTimer() {
window.clearTimeout(timeoutID);
goActive();
}
function goInactive() {
console.log('Ahhhh.. finally inactive again. Now I may rest -_-');
outsideRouter.navigate(['login']);
}
function goActive() {
console.log('Active again');
startTimer();
}
function setupInactivityChecker() {
addEventListener("mousemove", resetTimer, true);
addEventListener("mousedown", resetTimer, true);
addEventListener("keypress", resetTimer, true);
addEventListener("DOMMouseScroll", resetTimer, true);
addEventListener("mousewheel", resetTimer, true);
addEventListener("touchmove", resetTimer, true);
addEventListener("MSPointerMove", resetTimer, true);
startTimer();
}
请注意,我的函数是在我的服务类之外调用的对于Ionic Angular,使用
@HostListener
是实现您想做的事情的最佳方法:
@HostListener('mousemove', ['$event'])
@HostListener('mousedown', ['$event'])
onEvent(event) {
this.resetTimer();
}
然后在构造函数中
或ngOnInit()
可以调用this.startTime()
对于检查不活动的用例,您可以在app.component.ts
文件中编写此代码,该文件将侦听AppComponent
中的所有指定事件,其子组件(对于常规角度或离子应用程序)将是整个应用程序
您可以在此处了解有关@HostListener
的更多信息: