How to simulate Thread.sleep in GWT?

2,654 views
Skip to first unread message

laredotornado

unread,
Jan 24, 2012, 3:49:31 PM1/24/12
to Google Web Toolkit
Hi,

I'm using GWT 2.4 and writing some test cases uses GwtTestCase. I
want to stall my program for a few seconds at certain points, but the
only way I've seen to do this (using a Timer) is not addressing my
needs. How do I pause my program for a certain number of seconds?

ps - The Timer function is not what I want because all commands after
the Timer's schedule command continue to run. Execution is
interrupted only when the timer goes off.

Jim Douglas

unread,
Jan 24, 2012, 4:25:34 PM1/24/12
to Google Web Toolkit
There's really no good answer. JavaScript isn't multi-threaded, so
there's no way to do anything like Java's Thread.sleep(). Since
there's no real equivalent, the answer always boils down to "what
precisely are you trying to accomplish?" You can try reviewing what
other people have suggested:

http://www.google.com/search?q=javascript+sleep
http://www.google.com/search?q=gwt+thread.sleep

Colin Alworth

unread,
Jan 24, 2012, 6:31:42 PM1/24/12
to google-we...@googlegroups.com
GwtTestCase has support for waiting until an asynchronous part of the test is complete - check out the delayTestFinish(int) and finishTest() methods.

Beyond that, there really is no way to generally pause execution. You can use one timer to watch another timer, and call cancel on it if it runs for too long, though because JavaScript is singly threaded, you can't cancel something already in progress. For that, you want to break up your command into smaller pieces (using something like the Scheduler and RepeatingCommand).

objectuser

unread,
Jan 24, 2012, 10:40:23 PM1/24/12
to google-we...@googlegroups.com
Could you just have the final part of the test be the firing of the timer?
Reply all
Reply to author
Forward
0 new messages