How to delay in GWT?

5,228 views
Skip to first unread message

simplename

unread,
Jan 24, 2008, 6:16:00 PM1/24/08
to Google Web Toolkit
Hey everyone,

I've got a for loop running on the client side that I wish to delay
1sec everytime before the next iteration is run. However the GWT timer
supports scheduling, and I can't use the Thread Timer because no
threads are allowed in GWT. What can I do?


Thank you.

rjcarr

unread,
Jan 24, 2008, 6:25:38 PM1/24/08
to Google Web Toolkit
Why can't you use the Timer?

Sure, you will likely have to reorganize your code a bit, but you
should be able to do it.

simplename

unread,
Jan 24, 2008, 6:32:20 PM1/24/08
to Google Web Toolkit
when you say the Timer, do you mean the built in Timer from Java
java.util.Timer??

simplename

unread,
Jan 24, 2008, 6:37:26 PM1/24/08
to Google Web Toolkit
because when I tried using that GWT wouldn't recognise it and a
runtime error appears:

java.util.Timer cannot be resolved to a type

Fred Sauer

unread,
Jan 24, 2008, 6:38:35 PM1/24/08
to Google-We...@googlegroups.com
Its com.google.gwt.user.client.Timer

So:
  Timer timer = new Timer() {
    public void run {
      // your code
    }
  };

Then:
  timer.schedule(1000);
  timer.scheduleRepeating(5000);
  timer.cancel();

rjcarr

unread,
Jan 24, 2008, 6:40:02 PM1/24/08
to Google Web Toolkit
Ahh ... that's your problem, try this instead:

http://google-web-toolkit.googlecode.com/svn/javadoc/1.4/com/google/gwt/user/client/Timer.html

(PS -- where did the old gwt javadocs go, I (mostly) liked those
better)

simplename

unread,
Jan 24, 2008, 6:46:28 PM1/24/08
to Google Web Toolkit
thanks fred, what if the code to be run each time is dependant on an
iteration variable say k?
do I go:

final k = 0;
Timer timer = new Timer() {
public void run {
// code dependant on k
k++;
}
};

Then:
timer.schedule(1000);
timer.scheduleRepeating(5000);
timer.cancel();


On Jan 25, 9:38 am, "Fred Sauer" <f...@allen-sauer.com> wrote:
> Its com.google.gwt.user.client.Timer
>
> So:
> Timer timer = new Timer() {
> public void run {
> // your code
> }
> };
>
> Then:
> timer.schedule(1000);
> timer.scheduleRepeating(5000);
> timer.cancel();
>
> On Jan 24, 2008 4:32 PM, simplename <nanasuiwestw...@gmail.com> wrote:
>
>
>
> > when you say the Timer, do you mean the built in Timer from Java
> > java.util.Timer??
>
> > On Jan 25, 9:25 am, rjcarr <rjc...@gmail.com> wrote:
> > > Why can't you use the Timer?
>
> > > Sure, you will likely have to reorganize your code a bit, but you
> > > should be able to do it.
>
> --
> Fred Sauer
> f...@allen-sauer.com

simplename

unread,
Jan 24, 2008, 6:47:02 PM1/24/08
to Google Web Toolkit
thanks for that =)
just trying out fred's way now

On Jan 25, 9:40 am, rjcarr <rjc...@gmail.com> wrote:
> Ahh ... that's your problem, try this instead:
>
> http://google-web-toolkit.googlecode.com/svn/javadoc/1.4/com/google/g...

Fred Sauer

unread,
Jan 24, 2008, 7:09:50 PM1/24/08
to Google-We...@googlegroups.com
Do get it to compile, something like:

    final int k = 0;


    Timer timer = new Timer() {
      int t = k;
      public void run() {
        // code dependent on t
        t++;
      }
    };


simplename

unread,
Jan 24, 2008, 7:45:57 PM1/24/08
to Google Web Toolkit
i've tried sth similar but its not working =/
the reason is all these is meant to run inside a method, meaning once
the method exits the timer no longer exists =/


On Jan 25, 10:09 am, "Fred Sauer" <f...@allen-sauer.com> wrote:
> Do get it to compile, something like:
>
> final int k = 0;
>
> Timer timer = new Timer() {
> int t = k;
> public void run() {
> // code dependent on t
> t++;
> }
> };
>
> --
> Fred Sauer
> f...@allen-sauer.com

chris.f.jones

unread,
Jan 24, 2008, 7:59:26 PM1/24/08
to Google Web Toolkit
You'll need to keep a handle on the timer outside the context of your
method. Its run method will be run each time the timer is up.
So, you'd set up the timer as posted above and then call:
timer.scheduleRepeating(1000);
and then it will do its job indefinitely until you call
timer.cancel();
which you can do from within the timer's run method if you have an
implicit exit condition.

Whatever you do, don't call timer.run();
Thats a common mistake ;)
Reply all
Reply to author
Forward
0 new messages