使用Javascript setTimeout调用MVC操作来保持活动表单身份验证
问题描述:
我使用setTimeout来调用MVC操作来尝试保持用户表单身份验证的活动状态。使用Javascript setTimeout调用MVC操作来保持活动表单身份验证
当我正常调用此代码(即不使用setTimeout)时,它会保持表单身份验证活着。
在setTimeout中调用时,它不会。
我的问题是为什么当通过setTimeout调用时不工作?
$(document).ready(function() {
DoKeepAlive(); //simple test when page loads....does keep forms auth alive
});
var timeout= 0;
timeout= setTimeout(validationPrompt, 2 * 60 * 1000);
function validationPrompt() {
var answer = confirm("Your session will timeout in less than a minute. Click OK to stay logged in, and reset the timout period.")
if (answer) {
DoKeepAlive(); //when called in here by setTimeout, does NOT keep forms auth alive
//re-set the 10 minutes count
clearTimeout(timeout);
timeout= setTimeout(validationPrompt, 2 * 60 * 1000);
}
else {
var URL = "<%= Url.Content("~/Account/Logoff") %>"
window.location = URL;
}
}
function DoKeepAlive(){
var URL = "<%= Url.Content("~/Account/ValidateAuthentication") %>"
$.post(
//"../../Account/ValidateAuthentication",
URL,
function(data) {
}
);
}
答
很难说为什么你的代码不工作。我会首先尝试简化它:
<script type="text/javascript">
window.setInterval(function() {
var answer = confirm("Your session will timeout in less than a minute. Click OK to stay logged in, and reset the timout period.");
if (answer) {
$.post('<%= Url.Action("ValidateAuthentication", "Account") %>');
} else {
window.location.href = '<%= Url.Action("Logoff", "Account") %>';
}
}, 2 * 60 * 1000);
</script>
缺少一些代码?什么是“promptUserTimeoutId”?另外,为什么你将“timeout”设置为“validationPrompt”函数? – 2011-06-10 07:43:17
对不起亚历杭德罗,我试图分类我的代码,并把它弄得一团糟 - 现在应该更清楚了吗? – ozz 2011-06-10 08:36:23