Javascript session timeout with popup alert for multiple tabs

前端 未结 2 1159
予麋鹿
予麋鹿 2021-01-14 08:40

I am using javascript setInterval() to check user idle time and show a popup alert before automatic logout. But it does not work for multiple tabs (working fine for single t

2条回答
  •  爱一瞬间的悲伤
    2021-01-14 09:10

    Thank you guys, I got the solution for this.

    I used a localStorage value with current time stored in it. If there is no value exists in localStorage["currentTime"], stored current time in localStorage .

    var currentTime         = new Date();
    
    if ( !(localStorage.getItem("currentTime")) || (localStorage.getItem("currentTime") == "") )
    {
            idleTime        = 0;
            setTimeout(function() { localStorage.setItem("currentTime", currentTime)},5000); // current time is set to localStorage after  seconds (it is for setting in multiple tabs)
    } 
    

    All calculations to show timeout popup is done using localStorage.getItem("currentTime") value.

    Then I set localStorage["currentTime"] to null if user is not idle (when user clicks somewhere)

    $(this).click(function (e) 
    {
        $('#timeoutWindow').modal('hide');
        localStorage.setItem("currentTime", "");
        idleTime = 0;
    });
    

提交回复
热议问题