2012-11-16 121 views
0

我試圖以5秒爲間隔運行'checkServer'。然而'服務器很好'只運行一次。重複該功能需要做什麼?Dart服務器檢查時間間隔

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

回答

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

謝謝。現在我明白了Timer語法。 – basheps