如何在Dart SDK 0.4+中使用setInterval / setTimeout [英] How to use setInterval/setTimeout in Dart SDK 0.4+
问题描述
我意识到在当前的Dart SDK版本0.4.1.0_r19425中,例如 setTimeout
, setInterval
, clearTimeout
, clearInterval
不是 Window
类,它们都移到了 WorkerContext
。
现在是否有任何有关如何使用它们的文档?每次我想使用它们时,都需要创建一个 WorkerContext
的新实例吗?
I realised that in current Dart SDK version 0.4.1.0_r19425 methods like setTimeout
, setInterval
, clearTimeout
, clearInterval
aren't part of Window
class any more and they all moved to WorkerContext
.
Is there any documentation on how to use them now? Do I need to create a new instance of WorkerContext
every time I want to use them?
推荐答案
除了克里斯提到的计时器外,还有一个基于未来的 API:
In addition to Timer mentioned by Chris, there is a Future-based API:
var future = new Future.delayed(const Duration(milliseconds: 10), doStuffCallback);
目前尚无直接支持取消Future回调的方法,但是效果很好:
There is not yet direct support for cancelling a Future callback, but this works pretty well:
var future = new Future.delayed(const Duration(milliseconds: 10));
var subscription = future.asStream().listen(doStuffCallback);
// ...
subscription.cancel();
希望很快会有一个 Timer.repeating的流版本。
这篇关于如何在Dart SDK 0.4+中使用setInterval / setTimeout的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!