0

I'm trying to run 'checkServer' at 5 second intervals. However 'server is fine' only runs once. What needs to be done to repeat the function?

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());
}

1 Answer 1

2

You have to give a void callback(Timer timer) as second parameter for Timer.repeating constructor.

With the following code, checkServer will be called every 5 seconds.

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);
}
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.