我使用这个jQuery代码来显示Session Timeout窗口(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>
答案 0 :(得分:3)
简而言之,您可以在页面上使用变量并在活动上更新,例如按键和鼠标点击。如果有活动,请执行ping以使服务器会话保持活动状态。
您可以看到此处演示的技术:How to keep alive a user's session while they are posting on a forum?
,而不是重新创建整个写入编辑:
为了帮助你,我花了很多时间并采用你借来的代码,对其进行大幅修改和重组,以便你能够更好地理解代码中发生的事情。这可以提高您进一步成功更新的机会。我没有花时间优化它,但它应该工作。我已经更新了小提琴,所以你可以尝试一下:
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");
}
);