7 Replies Latest reply on Oct 13, 2008 11:50 AM by Ilya Shaikovsky

    How to stop a4j:poll if user is 'away'?

    CN Yee Newbie

      Hi,

      I have a page that uses a4j:poll to do periodic refresh. I like to stop the refresh if the user is 'away' - for example no mouse mements for a period of time.

      I have implemented something similar using prototype.js, I am wondering what is the proper way to do this in a4j.

      Many thanks in advance.
      Cheers,
      Yee

        • 1. Re: How to stop a4j:poll if user is 'away'?
          Sergey Smirnov Master

          How you did it with prototype? I mean if you have an already working code, you can use a4j:jsFunction to perform the JSF Ajax call from the code you already have.

          • 2. Re: How to stop a4j:poll if user is 'away'?
            CN Yee Newbie

            It is basically taggig a onmouseover event to , the event will set a hidden field (activeFlag).

            Then there is a timer (initiated by setInterval()) for ajax form posting. The timer checks for the activeFlag before posting to the server, then clears the activeFlag.

            The crux is in the posting back part - if I can control the a4j:poll posting back conditioned on a field that would be sufficient.

            How about introduce a something like "stopIfInactive" attribute to a4j:poll:

            <a4j:poll stopIfInactive=20000 ...


            ------------------------------------------------------
            /* submit a form in ajax */
            function invoke(form, event, container) {
            var params = Form.serialize(form);
            if (event != null) params = event + '&' + params;
            new Ajax.Updater(container, form.action, method:'post', postBody:params});
            }

            function getTime() {
            if ($("activeFlag").value == "1") {
            invoke($("timerForm"), "getTime", $("sysTime"));
            $("activeFlag").value = "0";
            }
            }

            function init() {
            setInterval('getTime()',5000);
            document.getElementsByTagName("body")[0].onmouseover =
            function(event) {
            resetActiveFlag();
            }
            }

            function resetActiveFlag() {
            $("activeFlag").value = "1";
            }

            &lt;body onLoad="javascript:init();"&gt;
            ...

            • 3. Re: How to stop a4j:poll if user is 'away'?
              Sergey Smirnov Master

              <a4j:poll> has a onsubmit attribute. If the this function return false, the request will be canceled.
              So, you can reuse your "activeFlag" . Let onsubmit return false if this flag equals 0

              • 4. Re: How to stop a4j:poll if user is 'away'?
                CN Yee Newbie

                Thanks. That's quite obvious, now that you pointed it out.

                How about implementing an attribute in aj4:poll for this? I think this is a common enough requirements. The javascript to implement this is not trivial at all.

                Regards

                • 5. Re: How to stop a4j:poll if user is 'away'?
                  Sergey Smirnov Master

                   

                  "yeecn@pd.jaring.my" wrote:
                  How about implementing an attribute in aj4:poll for this?

                  I am negative. This is an application specific behavior.

                  • 6. Re: How to stop a4j:poll if user is 'away'?
                    russell logar Newbie

                     

                    "SergeySmirnov" wrote:
                    "yeecn@pd.jaring.my" wrote:
                    How about implementing an attribute in aj4:poll for this?

                    I am negative. This is an application specific behavior.


                    I disagree, controlling the polling that is controlled by the framework is a framework issue. Also, this is something that is pervasive across sites and applications. Making the developers remember to do this and do it consistently is tough. If the framework implements it, it gets very much easier, shortens development and test times and ensures that it happens in the first place. In my company, we have policies that deny auto-refresh for exactly the reason that we must time-out a session on USER inactivity. With no way to easily ensure that the coding in the client side is done correctly for every page, we say to turn refreshes off. If a framework like aj4 did this , we could re-think that position and give our users a better experience.

                    • 7. Re: How to stop a4j:poll if user is 'away'?
                      Ilya Shaikovsky Master

                      This is just your case. But lot of other users uses a4j:poll in order to prevent timeout while user is gone ;) So everyone need its own solution.