0

我正在尝试每隔 5 秒运行一次“checkServer”。但是“服务器很好”只运行一次。重复该功能需要做什么?

import 'dart:io';
import 'dart:uri';
import 'dart:isolate';

checkServer() {
  HttpClient client = new HttpClient();
  HttpClientConnection connection = client.getUrl(...);

  connection.onResponse = (res) {
    ...
    print('server is fine');
    //client.shutdown();
  };

  connection.onError = ...;
}

main() {
  new Timer.repeating(5000, checkServer());
}
4

1 回答 1

2

您必须为构造函数提供void callback(Timer timer)第二个参数。Timer.repeating

使用以下代码,checkServer将每 5 秒调用一次。

checkServer(Timer t) {
  // your code
}

main() {
  // schedule calls every 5 sec (first call in 5 sec)
  new Timer.repeating(5000, checkServer);

  // first call without waiting 5 sec
  checkServer(null);
}
于 2012-11-16T15:37:58.867 回答