Search code examples
javascriptangularng-idle

why to use ng-idle lib to detect user idle time, if we can write our own code in js to detect the user inactivity using key and mouse events


I am able to detect the user idle time using simple mouse and key event.as below.

@HostListener('window:keydown', ['$event'])
@HostListener('window:mousedown', ['$event'])
@HostListener('window:mousewheel', ['$event'])
refreshUserState() {`enter code here`
 clearTimeout(this.userActivity);
 this.setTimeout();
}

is this will cause any performance issue and advantage of using ng-idle over this approach?


Solution

  • ng-idle has a lot of events and timers to handle and detect if user idle. It depends on your project and requirements if you need `ng-idle*.

    In the end you can handle it with your code sample. But only mouse events/keyboard events can be not enough to detect each situation (think about mobile devices/touch).

    But in the end: You basically don't have performance issues if you use your code instead of ng-idle