import { Injectable } from '@angular/core'; import { Router } from '@angular/router'; import { SessionStore } from '../store/session.service'; @Injectable({ providedIn: 'root' }) export class InativityService { constructor( private router: Router, ) { var time; window.onload = resetTimer; window.onmousemove = resetTimer; window.onmousedown = resetTimer; // catches touchscreen presses as well window.ontouchstart = resetTimer; // catches touchscreen swipes as well window.onclick = resetTimer; // catches touchpad clicks as well window.onkeydown = resetTimer; window.addEventListener('scroll', resetTimer, true); // improved; see comments function userIsNotActive() { // your function for too long inactivity goes here SessionStore.setInativity(false) try { window['inactivity/function']() } catch (error) {} } function resetTimer() { clearTimeout(time); time = setTimeout(userIsNotActive, 60000 * 15); // time is in milliseconds } } }