Reputation: 79
I am trying to implement session time out logic in my application. Application session is 60mins and I need to show user a message to extend if active session exceeds 60 minutes. Based on the session expiry minutes and user idle minutes I need to calculate the session expiry minutes.
I could see there are many implementation to get the user idle time out. However, I could not see any implementation to get the user idle minutes.
I am following the below implementation https://www.chunho-ling.com/2021/07/05/angular-idle-timeout-checking/
Can anyone shed some light on getting the idle minutes.
Upvotes: 0
Views: 2224
Reputation: 2108
I skimmed, but in general it looks like the implementation you linked should more or less do the trick.
The basics of it are:
a) Listen to (all/specific) user events such as mouse clicks, etc. at the document level
b) Keep a timeout timer running that gets reset whenever the above events are triggered.
c) Notify something when that timeout goes off
Your rather more specific question just involves saving a timestamp when those triggers fire and having that available to compare against.
private onUserEvent(): void {
this.resetTimer();
this.lastUserAction = new Date();
}
Then whatever you so desire to get your minutes since last action comparing the last action date vs current date (this should do it)
public get idleMinutes(): number {
return this.getMinutesBetween(this.lastUserAction, new Date());
}
Upvotes: 1