ng-inactivity-timer provides a service that keeps track of user activity based on custom activity monitors.
Install the package by running
npm install ng-inactivity-timer
{
provide: INACTIVITY_CONFIG,
useValue: <InactivityConfig>{
inactivityTime: 10,
warningTime: 10
}
}
export class ActivityMonitorService implements ActivityMonitor {
public getMonitor() {
return merge(
// Add any events you would like to monitor.
// These events are just examples.
fromEvent(document, 'keyup'),
fromEvent(document, 'keydown'),
fromEvent(document, 'keypress'),
fromEvent(document, 'mousemove'),
fromEvent(document, 'click'),
fromEvent(document, 'mousescroll'),
fromEvent(document, 'mouseup'),
fromEvent(document, 'mousedown')
).pipe(mapTo(undefined));
}
constructor() {}
}
{
provide: ACTIVITY_MONITOR,
useClass: ActivityMonitorService
}
export class AppComponent {
constructor(private inactivityTimerService: InactivityTimerService) {}
}
// Start monitoring
// if called with true, will also trigger actvivity
this.inactivityTimerService.startMonitor();
// Stop monitoring
this.inactivityTimerService.stopMonitor();
// Get an observable emitting Timout objects describing the activity status
this.inactivityTimerService.getTimeOut().subscribe(activity => {
// do something
});
// Trigger an activity (other than the ones provided by the monitor)
this.inactivityTimerService.activate();
The getTimeOut()
function emits objects of the Timeout interface:
export interface Timeout {
showWarning: boolean;
timedOut: boolean;
timeLeft: number;
}