GWT中的Java Timer

Igo*_*nko 4 java gwt html5 libgdx

我正在尝试在我的EntryPoint中安装Java Timer:

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

但是当我尝试编译时,我得到了:没有可用于类型的源代码java.util.Timer; 你忘了继承一个必需的模块吗?

我该怎么做才能解决这个错误?

Sur*_*tta 12

在GWT中,您只能使用所有Util包类.

这是只能从util类中使用的类列表.

您可以使用GWT Timer类.

示例(来自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);
  }
Run Code Online (Sandbox Code Playgroud)


P.T*_*.T. 5

如果您使用的是Libgdx,则可以使用libgdx Timer基础结构来安排将来运行的工作:

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

这样,您可以在与平台无关的代码中安排计时器.(特定于GWT的解决方案仅适用于项目中与平台相关的部分.)