GWT 中的倒计时时钟

发布于 2024-11-24 02:08:28 字数 348 浏览 1 评论 0 原文

我想在 GWT 中创建一个倒计时时钟,但我找不到等待一秒钟的正确函数。我尝试使用 Thread.Sleep() 但我认为这是出于另一个目的。 你能帮助我吗?这是我的代码。

    int count=45;

    RootPanel.get("countdownLabelContainer").add(countdown);
    for(int i=count; i>=0; i--)
    {
        countdown.setText(Integer.toString(i));
        // Place here the wait-for-one-second function
    }

I want to create a countdown clock in GWT but I cannot find the right function that waits for one second. I tried with Thread.Sleep() but I think it is for another purpose.
Can you help me? This is my code.

    int count=45;

    RootPanel.get("countdownLabelContainer").add(countdown);
    for(int i=count; i>=0; i--)
    {
        countdown.setText(Integer.toString(i));
        // Place here the wait-for-one-second function
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

夏末 2024-12-01 02:08:30

尝试一下 Timer (请参见此处)。

将示例代码快速更改为接近您想要的内容,但您会希望根据您的目的对其进行增强:

public class TimerExample implements EntryPoint, ClickListener {
  int count = 45;

  public void onModuleLoad() {
    Button b = new Button("Click to start Clock Updating");
    b.addClickListener(this);
    RootPanel.get().add(b);
  }

  public void onClick(Widget sender) {
    // Create a new timer that updates the countdown every second.
    Timer t = new Timer() {
      public void run() {
        countdown.setText(Integer.toString(count));
        count--;
      }
    };

    // Schedule the timer to run once every second, 1000 ms.
    t.schedule(1000);
  }
}

这听起来像是您正在寻找的一般区域中的内容。请注意,您可以使用timer.cancel() 来停止计时器。您需要将其与您的计数联系起来(当 45 达到 0 时)。

Give Timer a try (See Here).

Changing the example code real quick to something close to what you want, you'll want to buff this up for your purposes though:

public class TimerExample implements EntryPoint, ClickListener {
  int count = 45;

  public void onModuleLoad() {
    Button b = new Button("Click to start Clock Updating");
    b.addClickListener(this);
    RootPanel.get().add(b);
  }

  public void onClick(Widget sender) {
    // Create a new timer that updates the countdown every second.
    Timer t = new Timer() {
      public void run() {
        countdown.setText(Integer.toString(count));
        count--;
      }
    };

    // Schedule the timer to run once every second, 1000 ms.
    t.schedule(1000);
  }
}

This sounds like something in the general area of what your looking for. Note that you can use timer.cancel() to stop the timer. You'll want to tie this in with your count (when 45 hits 0).

十秒萌定你 2024-12-01 02:08:30

下面的代码片段显示了计时器的使用也有效。它展示了如何正确安排计时器以及如何取消它。

 // Create a new timer that updates the countdown every second.
    Timer t = new Timer() {
        int count = 60; //60 seconds
      public void run() {
        countdown.setText("Time remaining: " + Integer.toString(count) + "s.");
        count--;
        if(count==0) {
            countdown.setText("Time is up!");
            this.cancel(); //cancel the timer -- important!
        }
      }
    };

    // Schedule the timer to run once every second, 1000 ms.
    t.scheduleRepeating(1000); //scheduleRepeating(), not just schedule().

The following snippet showing the use of the timer works too. It shows how to schedule the timer properly and how to cancel it.

 // Create a new timer that updates the countdown every second.
    Timer t = new Timer() {
        int count = 60; //60 seconds
      public void run() {
        countdown.setText("Time remaining: " + Integer.toString(count) + "s.");
        count--;
        if(count==0) {
            countdown.setText("Time is up!");
            this.cancel(); //cancel the timer -- important!
        }
      }
    };

    // Schedule the timer to run once every second, 1000 ms.
    t.scheduleRepeating(1000); //scheduleRepeating(), not just schedule().
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文