2

In our web application, we have a baseTemplate.xhtml and every page of my application use this template.This baseTemplate.xhtml is having the functionality to handle Idle monitor.
Problem/Issue: If a user opens multiple tabs and one tab is active but another tab is idle for specified session timeout value, the user is going to be logged out.
Requirement: I want if a user opened multiple tabs and one of the tabs is active, the Idle monitor should not log out.

<h:form>
<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()"/>
</h:form>

<script type="text/javascript">
//<![CDATA[            
    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/>");
    });
//]]>
</script>
MostlyJava
  • 345
  • 3
  • 21
  • Use https://stackoverflow.com/questions/7389328/detect-if-browser-tab-has-focus to start/stop the other idle monitors if they don't have focus. Maybe you need to restart/reset them to. In the end, client-side it is all just html/css/javascript – Kukeltje Jun 21 '17 at 13:30
  • I just added source code, please can you help me to fix it, I am not good in java script – MostlyJava Jul 12 '17 at 18:49

1 Answers1

7

A bit late, but I hope it's useful for somebody. Primefaces's idleMonitor now has a multiWindowSupport property, so you would end up with something like this:

<p:idleMonitor timeout="5000" multiWindowSupport="true" onidle="PF('idleDialog').show()" />
Ihor Patsian
  • 1,288
  • 2
  • 15
  • 25
EleazarAG
  • 105
  • 1
  • 6