【发布时间】:2015-04-21 14:44:42
【问题描述】:
我有这个代码来完成这个任务:如果用户在 10 秒内没有任何活动(鼠标移动和按键),则显示一个对话框(Jquery UI 对话框)来警告并让用户知道他们将在下一个页面被重定向到另一个页面5秒,除非他们做某事。如何在“您将在 5 秒后自动重定向到...”中设置“5”。倒计时为 5, 4, 3, 2, 1 => 然后重定向?
<script>
// Set timeout variables.
var timoutWarning = 10000; // Display warning in 10 seconds.
var timoutNow = 15000; // Timeout in 15 seconds
var logoutUrl = 'http://google.com'; // URL to redirect to.
var warningTimer;
var timeoutTimer;
// Start timers.
function StartTimers() {
warningTimer = setTimeout("IdleWarning()", timoutWarning);
timeoutTimer = setTimeout("IdleTimeout()", timoutNow);
}
// Reset timers.
function ResetTimers() {
clearTimeout(warningTimer);
clearTimeout(timeoutTimer);
StartTimers();
$("#timeout").dialog('close');
}
// Show idle timeout warning dialog.
function IdleWarning() {
$("#timeout").dialog({
modal: true
});
}
// redirect the user.
function IdleTimeout() {
window.location = logoutUrl;
}
</script>
html
<style>
#timeout {
display: none;
}
</style>
<body onload="StartTimers();" onmousemove="ResetTimers();" onclick="ResetTimers();">
<form id="form1" runat="server" defaultbutton="DoNothing">
<asp:Button ID="DoNothing" runat="server" Enabled="false" Style="display: none;" />
<div class="page-container">
<div class="container">
Hold your mouse and don't press any key for 10 seconds, an dialog alert will be displayed.
<div id="timeout">
<h1>Session About To Timeout</h1>
<p>
You will be automatically redirected to google after 5 seconds.<br />
To stay on this page move your mouse or press any key.
</p>
</div>
</div>
</div>
</form>
【问题讨论】:
-
你指的是段落中的“5”吗? (“您将在 5 秒后自动重定向到 Google。”)
-
你的页面有几个错误:
Error: cannot call methods on dialog prior to initialization; attempted to call method 'close' -
@Shaggy 是的,这就是我提到的 5 个。
标签: javascript jquery asp.net jquery-ui