【发布时间】:2021-02-09 08:28:35
【问题描述】:
我想使用一个简单的 p:idleMonitor 来处理一些逻辑。 但就我而言,我不想为 mousemove 重新启动 idlemonitors 计数器(仅用于单击等)
这可以通过 javascript 或其他方式实现吗? idlemonitor 没有该配置的任何属性。
【问题讨论】:
标签: javascript jsf primefaces
我想使用一个简单的 p:idleMonitor 来处理一些逻辑。 但就我而言,我不想为 mousemove 重新启动 idlemonitors 计数器(仅用于单击等)
这可以通过 javascript 或其他方式实现吗? idlemonitor 没有该配置的任何属性。
【问题讨论】:
标签: javascript jsf primefaces
将此代码添加到 JS 文件中,该文件在 PrimeFaces 之后加载到 MonkeyPatch 您的 IdleMonitor 以仅在 keydown 和 click 事件上停止。
关键是这一行events: "keydown click" // define active events
这是基于 PrimeFaces 8。
if (PrimeFaces.widget.IdleMonitor) {
PrimeFaces.widget.IdleMonitor.prototype.init = function(cfg) {
PrimeFaces.widget.BaseWidget.prototype.init.call(this, cfg);
var $this = this;
$(document).on("idle.idleTimer" + this.cfg.id, function() {
if ($this.cfg.onidle) {
$this.cfg.onidle.call($this);
}
$this.callBehavior('idle');
})
.on("active.idleTimer" + this.cfg.id, function() {
if ($this.cfg.onactive) {
$this.cfg.onactive.call($this);
}
$this.callBehavior('active');
});
var opts = {
idle: false, // indicates if the user is idle
timeout: this.cfg.timeout, // the amount of time (ms) before the user is considered idle
events: "keydown click" // define active events
};
$.idleTimer(opts, document, this.cfg.id);
if (cfg.multiWindowSupport) {
var globalLastActiveKey = $this.cfg.contextPath + '_idleMonitor_lastActive' + this.cfg.id;
// always reset with current time on init
localStorage.setItem(globalLastActiveKey, $(document).data('idleTimerObj' + this.cfg.id).lastActive);
$this.timer = setInterval(function() {
var idleTimerObj = $(document).data('idleTimerObj' + $this.cfg.id);
var globalLastActive = parseInt(localStorage.getItem(globalLastActiveKey));
var localLastActive = idleTimerObj.lastActive;
// reset local state
if (globalLastActive > localLastActive) {
// pause timer
$.idleTimer('pause', document, $this.cfg.id);
// overwrite real state
idleTimerObj.idle = false;
idleTimerObj.olddate = globalLastActive;
idleTimerObj.lastActive = globalLastActive;
idleTimerObj.remaining = $this.cfg.timeout;
// resume timer
$.idleTimer('resume', document, $this.cfg.id);
}
// update global state
else if (localLastActive > globalLastActive) {
localStorage.setItem(globalLastActiveKey, localLastActive);
}
}, 2000);
}
}
};
【讨论】: