Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Javascript clearTimeout Not Working Properly

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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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");
    }
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading