0

我使用这个 jQuery 代码来显示一个会话超时窗口 ( http://jsfiddle.net/xHEF9/515/ ) 问题是当用户在页面中停留的时间更长然后 timoutValue 时,窗口会弹出。

我需要找到一种方法来重置用户在页面上执行的每个活动的会话。

我将不胜感激。

这是我使用的代码:

<script type="text/javascript">        //<![CDATA[
         var timeout=<%#(int)HttpContext.Current.Session.Timeout%>*60*1000;
          //var timeout=2*60*1000;

         var uservar = "<%# Session["userID"]%>";
         $(window).load(function () {

             (function (a) {
                 jQuery.sessionTimeout = function (b) {
                     function f(a) {
                         switch (a) {
                             case "start":
                                 window.onbeforeunload = null;
                                 redirTimer = setTimeout(function () {
                                     window.location = d.redirUrl
                                 }, d.redirAfter - d.warnAfter);
                                 break;
                             case "stop":
                                 clearTimeout(redirTimer);
                                 break
                         }
                     }

                     function e(b) {
                         switch (b) {
                             case "start":
                                 //window.onbeforeunload = null;
                                 dialogTimer = setTimeout(function () {
                                     a("#sessionTimeout-dialog").dialog("open");
                                     f("start")
                                 }, d.warnAfter);
                                 break;
                             case "stop":
                                 clearTimeout(dialogTimer);
                                 break
                         }
                     }
                     var c = {

                         message: "Your session is about to expire.",
                         keepAliveUrl: "default.aspx",
                         redirUrl: "<%# Page.ResolveClientUrl("~/LogOut.aspx?user=")%>"+uservar,
                         logoutUrl: "<%# Page.ResolveClientUrl("~/LogOut.aspx?user=")%>"+uservar,
                        warnAfter: 9e5,
                        redirAfter: 12e5
                    };
                    var d = c;
                    if (b) {
                        var d = a.extend(c, b)
                    }
                    a("body").append('<div title="Session Timeout" id="sessionTimeout-dialog">' + d.message + "</div>");
                    a("#sessionTimeout-dialog").dialog({
                        autoOpen: false,
                        width: 400,
                        modal: true,
                        closeOnEscape: false,
                        open: function (b, c) {
                            a(".ui-dialog-titlebar-close").hide()
                        },
                        buttons: {
                            "Log Out Now": function () {
                                window.location = d.logoutUrl
                            },
                            "Stay Connected": function () {
                                //this event is triggered when the user tries to leave the page
                                window.onbeforeunload = confirmExit;
                                a(this).dialog("close");

                                a.ajax({
                                    type: "POST",
                                    url: d.keepAliveUrl
                                });
                                f("stop");
                                e("start")
                            }
                        }
                    });
                    e("start")
                }
            })(jQuery)
            $(document).ready(function () {

                $.sessionTimeout({
                    warnAfter: timeout-(60*1000)
                    ,redirAfter: timeout
                });
            });
        });//]]>  
</script>
4

1 回答 1

3

简而言之,您可以在页面上使用变量并根据活动(例如按键和鼠标单击)对其进行更新。如果有活动,请执行 ping 以保持服务器会话处于活动状态。

您可以在这里查看演示的技术,而不是重新创建完整的文章:如何在用户在论坛上发帖时保持用户会话的活跃?


编辑:
为了帮助您,我花费了大量时间并获取了您借用的代码,对其进行了大量修改和重组,以便您应该能够更好地理解代码中发生的事情。这应该会提高您进一步成功更新它的机会。我没有花时间优化它,但它应该可以工作。我已经更新了小提琴,所以你可以试试:http:

//jsfiddle.net/xHEF9/555/

并且还包括下面的更新代码。

var activityTimer, warningTimer, redirTimer;
var haveNewInput = false;
var inactivityParms = {
    warningMessage: "Your session is about to expire.",
    keepAliveUrl: "/keep-alive",
    redirUrl: "/timed-out",
    logoutUrl: "/log-out",
    activityCheckEvery: 3000, //  3 sec * 1000 ms
    warnAfter: 10000,         // 10 sec * 1000 ms
    redirAfter: 20000         // 20 sec * 1000 ms
};


// Function to check for keyboard input and reset timers if input is detected
function setActivityCheckTimer(activityCheckTimerAction) {
    switch (activityCheckTimerAction) {
        case "start":
            activityTimer = setInterval(
                function () {
                    if (haveNewInput == true) {
                        pingServerToKeepSessionAliveAndResetTimers();
                        haveNewInput = false;
                    }
                },
                inactivityParms.activityCheckEvery);
            break;
        case "stop":
            clearTimeout(activityTimer)
    }
}

function setWarningTimer(warningTimerAction) {
    switch (warningTimerAction) {
        case "start":
            warningTimer = setTimeout(
                function () {
                    $("#sessionTimeout-dialog").dialog("open");
                    setActivityCheckTimer("stop");
                    setRedirTimer("start");
                },
                inactivityParms.warnAfter);
            break;
        case "stop":
            clearTimeout(warningTimer)
    }
}


function setRedirTimer(redirTimerAction) {
    switch (redirTimerAction) {
        case "start":
            redirTimer = setTimeout(
                function () {
                    window.location = inactivityParms.redirUrl
                },
                inactivityParms.redirAfter - inactivityParms.warnAfter);
            break;
        case "stop":
            clearTimeout(redirTimer)
    }
}


function AppendWarningMessage() {
    $("body").append('<div title="Session Timeout" id="sessionTimeout-dialog">' + inactivityParms.warningMessage + "</div>");
    $("#sessionTimeout-dialog").dialog({
        autoOpen: false,
        width: 400,
        modal: true,
        closeOnEscape: false,
        open: function () {
            $(".ui-dialog-titlebar-close").hide()
        },
        buttons: {
            "Log Out Now": function () {
                window.location = inactivityParms.logoutUrl
            },
            "Stay Connected": function () {
                $(this).dialog("close");
                pingServerToKeepSessionAliveAndResetTimers();
            }
        }
    });
}

function pingServerToKeepSessionAliveAndResetTimers() {
    setRedirTimer("stop");
    setWarningTimer("stop");
    setActivityCheckTimer("stop");
    $.ajax({ type: "POST", url: inactivityParms.keepAliveUrl });
    setActivityCheckTimer("start");
    setWarningTimer("start");
}

function DetectInputEvents() {
    $(document).keydown(function () {
        haveNewInput = true;
    });
}

(jQuery);

$(document).ready(
    function () {
        AppendWarningMessage();
        DetectInputEvents();
        setActivityCheckTimer("start");
        setWarningTimer("start");
    }
);
于 2013-08-10T23:21:31.697 回答