On Fri, Jan 7, 2011 at 4:56 PM, Nelson Segura <nsegu...@gmail.com> wrote:

> 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;
>            }
>
>        });
>

Are you adding this to *every* component that you want to track?  It's hard
to tell, but the method signature of your checkKeyPress makes me think you
might be.

Although not directly answering your question, I'd actually do it the other
way around:

1 - use an ajax behavior (not a timer) and wrap it in a custom function like
"startKeepAliveTimer()".
1b - in the onRespond of this behavior, simply add a new ajax timer to the
page and let it run continuously
2 - then any component that can accept key presses that should trigger a
keep alive timer can have a normal (non-ajax) behavior attached to them that
appends onkeypress="startKeepAliveTimer()" to their tag.  (this could even
be done automagically via component instantiation listener, or IVisitor,
etc)

This is an easy way to have a single AJAX keep-alive timer for all of your
components on the page.

-- 
Jeremy Thomerson
http://wickettraining.com
*Need a CMS for Wicket?  Use Brix! http://brixcms.org*

Reply via email to