I am writing code that checks whether the user has typed something in the
page, and if so, goes an AJAX call to keep the user session alive.
For this I am using AbstractAjaxTimerBehavior to ping the server every
certain time, and overriding its getPreconditionScript() method to call a JS
function that knows if the user has typed something since the last AJAX
call.

This works well as long as the user types something between the time calls,
but if ever the getPreconditionScript() determines the user has not typed
anything, then the timer will stop.
I would expect the AJAX call not to be made, but the timer to continue to
run for the next time, so if the user types something, then the call will be
done in the next run.

What is the correct behavior in this case? Do I have a bug, or is it working
as written? If the second, then any ideas how to avoid the timer from
stopping?

Code is below. Thanks for your help.

-Nelson

-----
           add(new AbstractAjaxTimerBehavior(Duration.seconds(20)) {
            private static final long serialVersionUID =
-4426283634345968585L;

            @Override
            protected void onTimer(AjaxRequestTarget target) {
                target.focusComponent(null);
            }

            @Override
            protected CharSequence getPreconditionScript() {
                StringBuilder sb = new StringBuilder();
                sb.append("if
(checkKeyPress('").append(getMarkupId()).append("')) { return true } else {
return false }");
                return sb;
            }

        });

Reply via email to