【发布时间】:2017-01-31 06:44:35
【问题描述】:
代码在 MVC 中,而且每个请求都使用 ajax 调用,所以 url 没有变化。
如果用户不活动 30 秒,我将能够使用以下代码执行注销用户的首次操作。但在用户注销时无法执行操作。
<script>
$(function () {
$("body").on('click keypress', function () {
ResetThisSession();
});
});
var timeInSecondsAfterSessionOut = 30; // to change the session time out, change this value. Must be in seconds.
var secondTick = 0;
function ResetThisSession() {
secondTick = 0;
}
function StartThisSessionTimer() {
secondTick++;
console.log(secondTick);
if (secondTick > timeInSecondsAfterSessionOut) {
clearTimeout(tick);
window.location = '/Account/LogOff/0';
}
tick = setTimeout("StartThisSessionTimer()", 1000);
}
StartThisSessionTimer();
</script>
另外我尝试了 unload 或 beforeunload 方法的脚本,但结果不正确。
如果用户在 30 秒内未执行任何操作或用户关闭浏览器,则需要注销用户。
提前致谢。
【问题讨论】:
标签: javascript jquery asp.net-mvc