I have written this function which calls a .net method if there is no user activity on the page for sometime.
function IdleSessionLogout(dotnetHelper) {
var timer;
document.onmousemove = resetTimer;
document.onkeypress = resetTimer;
function resetTimer() {
console.log("I am in Reset Timer Function");
clearTimeout(timer);
timer = setTimeout(Logout, 5000);
}
function Logout() {
dotnetHelper.invokeMethodAsync("Logout");
}
}
I have checked function is triggered but the clearTimeout does not work and user is still logged out even there is some activity from user. Can please tell me what I am doing wrong here?
2
Answers
I created the timeout outside of the reset timer, if the user moves the mouse clearTimeout will invoke and log out operation will cancel. Try this;
Your code works as-is:
So then the issue (assuming
dotnetHelper.invokeMethodAsync("Logout")
works as expected) is that you’re setting an event listener with=
instead of.addEventListener
. Only one event listener can be enabled at a time if you use=
, so if you have an=
somewhere after this, the event listeners will be removed.You should also initialize the timer so that if there is never any mouse movement or keypress that it properly calls the
Logout
function.