我试图运行' checkServer'以5秒的间隔。但是服务器很好'只运行一次。重复这个功能需要做些什么?
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());
}
答案 0 :(得分:2)
您必须为void callback(Timer timer)
构造函数提供Timer.repeating
作为第二个参数。
使用以下代码,将每5秒调用checkServer
。
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);
}