如何在 GWT 中循环 AsyncAjaxRequest?

How can I loop AsyncAjaxRequest in GWT?

我需要每 10 秒发出一个 ajax 请求并在客户端更新数据。

所以,我在我的 onModuleLoad() 中尝试过这种方式:

while (true) {
    try {
        someService.initTable(new AsyncCallback<SomeObject>() {

                    @Override
                    public void onFailure(Throwable caught) {
                    }

                    @Override
                    public void onSuccess(SomeObject result) {
                        initData(numbersTable, result);
                    }
                });
            } catch (Exception e) {
            }
        }

但是会进入无限循环。

我想要这样的东西

(function worker() {
  $.ajax({
    url: 'ajax/test.html', 
    success: function(data) {
      $('.result').html(data);
    },
    complete: function() {
      // Schedule the next request when the current one's complete
      setTimeout(worker, 5000);
    }
  });
})();

gwt 中的异步调用立即执行,return"later"。在你的代码中,while 循环不等待任何东西,所以你每秒调用 initTable() 很多次,因此是无限循环。

只需创建一个每 10 秒执行一次的计时器。

final Timer timer = new Timer() {

        @Override
        public void run() {
              try {
                someService.initTable(new AsyncCallback<SomeObject>() {

                     @Override
                     public void onFailure(Throwable caught) {
                     }

                     @Override
                     public void onSuccess(SomeObject result) {
                         initData(numbersTable, result);
                     }
                   });
                } catch (Exception e) {
                }
        }

};
timer.scheduleRepeating(10000);