我编写了像下面的代码行的jquery
<script type="text/javascript">
var t;
window.onload=resetTimer;
document.onkeypress = resetTimer;
document.onmouseover = resetTimer;
function logout() {
var answer = confirm('You will be logged out after 60 Second');
if (answer) {
location.href = '../login/login.aspx'
}
else {
window.onload = resetTimer;
}
}
function resetTimer()
{
clearTimeout(t);
t=setTimeout(logout,10000) //logs out in 10 min
}
</script>
现在我希望如果用户在 60 秒后未确认,则应在 60 秒后注销,即应将其重定向到登录页面。请帮忙 我!!!
最佳答案
confirm()
会阻止所有其他 Javascript,因此当用户响应时间超过 60 秒时您无法取消它。
您可以使用 jQuery UI 对话框来完成此操作。
$(function() {
resetTimer();
document.onkeypress = resetTimer;
document.onmouseover = resetTimer;
function logout() {
var t1 = setTimeout(reallyLogout, 60000);
$("<div>You will be logged out after 60 seconds</div>").dialog({
modal: true,
buttons: {
OK: reallyLogout,
Cancel: function() {
clearTimeout(t1);
resetTimer();
$(this).dialog("close");
}
}
});
}
var t;
function resetTimer() {
clearTimeout(t);
t = setTimeout(logout, 10 * 60000); // Logout in 10 minutes
}
function reallyLogout() {
location.href = '../login/login.aspx';
}
});
在演示中,我将注销计时器更改为 20 秒,将确认超时更改为 10 秒,以便您可以在合理的时间内进行测试。
关于javascript - jquery 如何在 60 秒后注销?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32243320/