会话超时不能通过使用javascript工作

时间:2017-01-05 10:50:00

标签: javascript c# jquery asp.net-mvc

我有下面的脚本。当时间为60秒或更短时,会出现弹出窗口(询问用户是否注销或继续),当我从弹出窗口中单击“继续”时,会重新加载页面。问题有时会被注销。不确定下面的代码有什么问题。任何帮助都是适当的。

var startTime = new Date();
var endTime = new Date(startTime.getTime() + (@FormsAuthentication.Timeout.TotalMilliseconds));
var i = 0;

function updateTimeoutDuration() {
        var diff = Math.floor((new Date() - endTime) / 1000) * -1;
        if (diff > 0) {
            minutes = Math.floor(diff / 60);
            seconds = Math.floor(diff % 60);
            $('#timeout-countdown #remaining-time').html(((minutes < 10) ? '0' : '') + minutes + ':' + ((seconds < 10) ? '0' : '') + seconds);
            console.log("Timeout - countdown",((minutes < 10) ? '0' : '') + minutes + ':' + ((seconds < 10) ? '0' : '') + seconds);
            $('#dialog-timeout-confirm #timeout-countdown-popup #remaining-time-popup').html(((minutes < 10) ? '0' : '') + minutes + ':' + ((seconds < 10) ? '0' : '') + seconds);

            // popup message will display when 60 sedconds remaining
            if (diff < 60) {

                // Autometically logged out when 0 second when timer will reach to 0 second
                if (diff < 1) {
                    $("#logoutfrm").submit();
                }

                $('#timeout-countdown #remaining-time').addClass('warning');
                $('#dialog-timeout-confirm #timeout-countdown-popup #remaining-time-popup').addClass('warning');
                //Popup message will open
                if (i == 0) {
                    i = 1;
                    popupMessage();
                }

                i = 1;
            }
        }
        else {
            window.location.href = '@FormsAuthentication.LoginUrl' + '?redirectUrl=' + encodeURIComponent(window.location.pathname);
        }
    }
    $(function () {
        setInterval(updateTimeoutDuration, 1000);
    });

 function popupMessage()
    {
        $("#dialog-timeout-confirm").dialog({
            resizable: false,
            height: 240,
            modal: true,
            buttons: [
                        {
                            text: "Log out",
                            "class": 'popup_logoutbtn',
                            click: function() {
                                var logoutfrm = document.getElementById('logoutForm');
                                if(logoutfrm != undefined && logoutfrm != null && logoutfrm != ''){
                                    //logoutfrm.submit();
                                    $("#logoutfrm").submit();
                                }
                                else{
                                    window.location.href = '@FormsAuthentication.LoginUrl' + '?redirectUrl=' + encodeURIComponent(window.location.pathname);
                                }
                            }
                        },
                        {
                            text: "Stay Logged In",
                            "class": 'popup_staylogoutbtn',
                            click: function() {
                                location.reload();
                            }
                        }
            ]
        });
    }

0 个答案:

没有答案