简体   繁体   English

GWT中的Java Timer

[英]Java Timer in GWT

I'm trying to have a Java Timer in my EntryPoint: 我正在尝试在我的EntryPoint中安装Java Timer:

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 ; 但是当我尝试编译时,我得到了:没有源代码可用于类型java.util.Timer ; did you forget to inherit a required module? 你忘了继承一个必需的模块吗?

What can I do to fix this error? 我该怎么做才能解决这个错误?

In GWT you are restricted to use all the Util package classes. 在GWT中,您只能使用所有Util包类。

Here is the List of Classes only you can use from util class. 这是只能从util类中使用的类列表

You can use GWT Timer class . 您可以使用GWT Timer类

Example( from docs ); 示例( 来自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);
  }

If you're using Libgdx, you can use the libgdx Timer infrastructure to schedule work to run in the future: 如果您正在使用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);

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.) (特定于GWT的解决方案仅适用于项目中与平台相关的部分。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM