我从这里使用JQuery idleTimeout插件:
http://www.erichynds.com/examples/jquery-idle-timeout/example-mint.htm
我在mvc 4应用程序中使用它。
下面是我设置会话计时器的代码片段。
<script type="text/javascript">
var sessionTimer = 60;
$(document).ready(function ()
{
// setup the dialog
$("#dialog").dialog({
autoOpen: false,
modal: true,
width: 400,
height: 210,
closeOnEscape: false,
draggable: false,
resizable: false,
buttons: {
'Yes, Keep Working': function () {
$(this).dialog('close');
},
'No, Logoff': function () {
// fire whatever the configured onTimeout callback is.
// using .call(this) keeps the default behavior of "this" being the warning
// element (the dialog in this case) inside the callback.
$.idleTimeout.options.onTimeout.call(this);
}
}
});
var $countdown = $("#dialog-countdown");
@* start the idle timer plugin *@
$.idleTimeout('#dialog', '#dialog-button-yes', {
idleAfter: (sessionTimer - 30),
keepAliveURL: '@Url.Action("KeepAlive", "Home")',
pollingInterval: 5,
serverResponseEquals: 'OK',
AJAXTimeout: 250 * 60,
onTimeout: function () {
window.location = '@Url.Action("Logout", "Login")';
},
onIdle: function () {
$(this).dialog("open");
},
onCountdown: function (counter) {
$countdown.html(counter); @* update the counter *@
}
});
});
此代码位于最外层/共享视图中。使用jquery $ .ajax使用部分视图加载我的所有页面。上面的代码只加载一次,sessionTimer设置为60秒。因此,当调用ajax post加载新页面时,计时器不会重置。即使用户处于活动状态,计时器也会在帖子之间打勾。 每次发生ajax帖子时,我有办法重置计数器吗? 我可以在每个内部视图$ .ajax成功条件下重置此项。但是有太多的地方。我想知道是否有一个可以在我的母版页上编写的公共代码,这将让我知道已经放置了一个ajax调用并重置了计数器。
答案 0 :(得分:0)
尝试使用jQuery中的.ajaxSuccess()
事件处理程序。您可以在此处查看有关如何使用它的文档:http://api.jquery.com/ajaxsuccess/。