Saturday, November 27, 2010

Re: Repeating Timer and onUnload()

On 23 nov, 17:26, Max Jonas Werner <m...@maxwerner.de> wrote:
> Hi,
>
> I have built a widget for my GWT application that shows the latest
> actions users of the application have performed. To refresh this
> widget automatically I use a Timer and its schedule() method like
> this:
>
>     private final Timer t = new Timer() {
>         @Override
>         public void run() {
>             // perform RPC call here to refresh the list of activities
>             schedule(10000);
>         }
>     };
>     ...
>     t.schedule(10000);
>
> I'm using schedule() here since scheduleRepeating() could lead to
> shorter intervalls which I don't want to. However, I could also have
> used scheduleRepeating() here, that's not the actual problem.
>
> My problem/question is rather: When this widget is removed from the
> DOM I'll have to cancel() the timer, so I override onUnload() and call
> t.cancel() in there. Is this the method you would recommend or is
> there some other fancy way of cancelling timers automatically when
> widgets are unloaded/detached?

Starting with GWT 2.1 you can add an AttachEvent.Handler to be
informed when a widget is attached/detached:
http://google-web-toolkit.googlecode.com/svn/javadoc/2.1/com/google/gwt/user/client/ui/Widget.html#addAttachHandler(com.google.gwt.event.logical.shared.AttachEvent.Handler)
But if your timer is instantiated as part of your widget, I'd go with
overriding onUnload rather than attaching an event handler on itself.

--
You received this message because you are subscribed to the Google Groups "Google Web Toolkit" group.
To post to this group, send email to google-web-toolkit@googlegroups.com.
To unsubscribe from this group, send email to google-web-toolkit+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/google-web-toolkit?hl=en.

No comments:

Post a Comment