Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java Timer in GWT

I'm trying to have a Java Timer in my EntryPoint:

Timer timer = new Timer();
timer.schedule(
   new TimerTask() {
       public void run() {
           //some code
       }
   }
   , 5000);

But when trying to compile this I got: No source code is available for type java.util.Timer; did you forget to inherit a required module?

What can I do to fix this error?

like image 662
Igor Kostenko Avatar asked Nov 30 '22 21:11

Igor Kostenko


2 Answers

In GWT you are restricted to use all the Util package classes.

Here is the List of Classes only you can use from util class.

You can use GWT Timer class.

Example(from docs);

 public void onClick(ClickEvent event) {
    // Create a new timer that calls Window.alert().
    Timer t = new Timer() {   //import (com.google.gwt.user.client.Timer)
      @Override
      public void run() {
        Window.alert("Nifty, eh?");
      }
    };

    // Schedule the timer to run once in 5 seconds.
    t.schedule(5000);
  }
like image 143
Suresh Atta Avatar answered Dec 03 '22 11:12

Suresh Atta


If you're using Libgdx, you can use the libgdx Timer infrastructure to schedule work to run in the future:

Timer t = new Timer();
t.scheduleTask(new Timer.Task() { 
      public void run() { /* some code */ } 
  }), /* Note that libgdx uses float seconds, not integer milliseconds: */ 5);

This way you can schedule the timer in your platform independent code. (The GWT-specific solution will only work in the platform dependent part of your project.)

like image 40
P.T. Avatar answered Dec 03 '22 10:12

P.T.