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?
>Solution :
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;
function IdleSessionLogout(dotnetHelper) {
var timer = setTimeout(Logout, 5000);
document.onmousemove = resetTimer;
document.onkeypress = resetTimer;
function resetTimer() {
console.log("I am in Reset Timer Function");
clearTimeout(timer);
}
function Logout() {
dotnetHelper.invokeMethodAsync("Logout");
}
}