gpt4 book ai didi

javascript - PrimeFaces confirmDialog 中的倒数计时器

转载 作者:行者123 更新时间:2023-11-29 23:46:50 28 4
gpt4 key购买 nike

在我的 Web 应用程序中,我有一个空闲监视器,如果用户空闲 5 分钟就会触发该监视器。它将打开一个确认对话框,等待 2 分钟;之后它将重定向到登录页面。

要求:我想显示一个倒数计时器,其中包含用户将被重定向到登录页面之前的剩余时间。

baseTemplate.xhtml:

<h:form>

<p:idleMonitor timeout="#{idleMonitorView.timeoutVal}"
onidle="startTimer()" />

<p:confirmDialog id="confirmDialog"
message="You have been idle, Please click ok to remain logged in"
header="Are you there?" severity="alert" widgetVar="idleDialog">
<p:commandButton id="confirm" value="Ok" oncomplete="extendSession()" />

</p:confirmDialog>

<p:remoteCommand name="terminateIdleSession" actionListener="#{idleMonitorView.onIdle}" out="count" />

<script type="text/javascript">
var extend_session = 0;
function startTimer() {

extend_session = 0;
setTimeout("timeout()", 120000);
PF('idleDialog').show();
}

function timeout() {
if (extend_session == 0) {
terminateIdleSession();
}
}

function extendSession() {
extend_session = 1;
PF('idleDialog').hide();
}
</script>
</h:form>

问题:我不知道如何实现这个要求。

最佳答案

您应该创建一个 JavaScript 间隔,它每秒更新一次计时器(或您需要的任何其他间隔)。您可以使用一些 jQuery 来更新您的计时器。我建议在对话消息中添加一个跨度,您可以将其用作计时器:

$("#myForm\\:confirmDialog .ui-confirm-dialog-message").append("<span id=logoffTimeout/>");

您应该将 myForm 替换为您在此处的表单中使用的 ID。

显示对话框时,计算注销计时器到期的时间并将其存储在例如 window.logoffTime 中。现在您可以使用以下函数来更新计时器:

function updateTimer(){
var seconds = Math.ceil((window.logoffTime - new Date().getTime()) / 1000);
$("#logoffTimeout").html(seconds);
}

在你的按钮和远程命令上,我建议使用 process="@this"。另请参阅:Understanding PrimeFaces process/update and JSF f:ajax execute/render attributes

您应该知道,当您在 JavaScript 中启动超时时,会返回一个 ID。您可以使用该 ID 来清除超时。

我最终得到了这个 XHTML:

<p:confirmDialog id="confirmDialog"
message="Please click Ok before the timer runs out: "
header="Are you there?"
severity="alert"
closable="false"
widgetVar="idleDialog">
<p:commandButton id="confirm"
value="Ok"
process="@this"
onclick="clearTimeout(window.logoffTimeoutId); PF('idleDialog').hide();"/>
</p:confirmDialog>

<p:remoteCommand name="terminateIdleSession"
actionListener="#{idleMonitorView.onIdle}"
process="@this"
out="count"/>

<p:idleMonitor timeout="#{5 * 60 * 1000}"
onidle="startTimer()"/>

还有这个 JavaScript:

function startTimer() {
clearTimeout(window.logoffUpdaterId);
PF('idleDialog').show();
// Set timeout to 2 minutes
var timeout = 2 * 60 * 1000;
// Calculate when the time runs out
window.logoffTime = new Date().getTime() + timeout;
// Start timer which calls remote command
window.logoffTimeoutId = setTimeout(terminateIdleSession, timeout);
// Update timer every second
window.logoffUpdaterId = setInterval(updateTimer, 1000);
// Update timer now
updateTimer();
}

// Update the timer
function updateTimer() {
var seconds = Math.ceil((window.logoffTime - new Date().getTime()) / 1000);
$("#logoffTimeout").html(seconds);
}

// Create span to contain the timer
$(function(){
$("#myForm\\:confirmDialog .ui-confirm-dialog-message").append("<span id=logoffTimeout/>");
});

我注意到您正在使用嵌入在 XHTML 中的 JavaScript。在这种情况下,添加 CDATA 以防止遇到 XML 错误:

<script type="text/javascript">
//<![CDATA[
...
//]]>
</script>

我建议从文件/资源​​加载您的脚本,这将为您带来缓存的好处。

关于javascript - PrimeFaces confirmDialog 中的倒数计时器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43685731/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com