関数を何度も実行したいのですが、その間に遅延があります。Dartでこれを行うにはどうすればよいですか?
7 に答える
このクラスを使用して、Timer
ワンショットおよび繰り返し機能をスケジュールできます。
繰り返す
繰り返し関数を実行する方法は次のとおりです。
import 'dart:async';
main() {
const oneSec = Duration(seconds:1);
Timer.periodic(oneSec, (Timer t) => print('hi!'));
}
Timer は、期間と実行する関数の 2 つの引数を取ります。期間は のインスタンスである必要がありますDuration
。コールバックは、単一のパラメーター (タイマー自体) を取る必要があります。
繰り返しタイマーのキャンセル
timer.cancel()
繰り返しタイマーをキャンセルするために使用します。これが、繰り返しタイマーからコールバック実行にタイマーが渡される理由の 1 つです。
遅れてワンショット
遅延後にワンショット関数をスケジュールするには (一度実行し、将来のある時点で):
import 'dart:async';
main() {
const twentyMillis = Duration(milliseconds:20);
Timer(twentyMillis, () => print('hi!'));
}
ワンショット タイマーのコールバックはパラメーターを取らないことに注意してください。
できるだけ早くワンショット
また、関数ができるだけ早く実行されるように要求することもできます。少なくとも 1 つのイベント ループ ティックの後に実行されます。
import 'dart:async';
main() {
Timer.run(() => print('hi!'));
}
HTMLで
タイマーは HTML でも機能します。実際にwindow.setTimeout
は が削除されたため、Timer は今後関数を実行する唯一の方法です。
https://api.dartlang.org/stable/1.24.3/dart-async/Stream/Stream.periodic.html
import 'dart:async';
StreamSubscription periodicSub;
void main() {
periodicSub = new Stream.periodic(const Duration(milliseconds: 500), (v) => v)
.take(10)
.listen((count) => print('tick $count'));
}
または、カウンターが必要ない場合
import 'dart:async';
StreamSubscription periodicSub;
void main() {
periodicSub = new Stream.periodic(const Duration(milliseconds: 500))
.take(10)
.listen((_) => print('tick'));
}
Future.delayed と await を使用して実行を遅らせることもできます。
Future<Null> delay(int milliseconds) {
return new Future.delayed(new Duration(milliseconds: milliseconds));
}
main() async {
await delay(500);
print('Delayed 500 milliseconds');
}
Timer.periodic や Stream.periodic とは反対に、このようなタスクを処理する私のお気に入りの方法を投稿します。利点:
- 最初のサイクルは即座に実行されます
- コールバックは、再入の問題が発生することなく、間隔よりも長く動作できます
Completer<bool> periodic(Duration interval, Function(int cycle) callback) {
final done = Completer<bool>();
() async {
var cycle = 0;
while (!done.isCompleted) {
try {
await callback(cycle);
} catch (e, s) {
log("$e", stackTrace: s);
}
cycle++;
await done.future
.timeout(interval)
.onError((error, stackTrace) => null);
}
}();
return done;
}
main() {
final task = periodic(Duration(seconds: 10), (cycle) async {
/// do the periodic tasks here
});
/// main code here
/// and when going to stop the above periodic call
task.complete(true);
}