We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Timer 属于 Dart 异步编程的一部分,通过 Timer 可以创建执行定时器(包括重复执行的)。
主要包含 Timer 和 Timer.periodic 两个构造函数:
Timer
Timer.periodic
Timer(Duration duration, void callback())
Timer.periodic(Duration duration, void callback(Timer timer))
分别对应 javascript 的 setTimeout 和 setInterval
setTimeout
setInterval
如 3 秒之后打印:
const timeout = Duration(seconds: 3); Timer(timeout, () { print('huruji'); });
1 秒重复打印:
Timer.periodic(Duration(seconds: 1), (Timer timer) { print('xie'); });
需要取消定时器,可以使用实例方法 .cancel
.cancel
int count = 0; Timer myTimer; myTimer = Timer.periodic(Duration(seconds: 1), (Timer timer) { print('xie'); if (count == 10) { myTimer.cancel(); } count++; });
Dart 和 js 一样都是单线程,所以为了能够让定时器尽快的调用,可以使用静态方法 run
run
Timer.run(callback)
这类似于将 setTimeout 的时间设置为 0
setTimeout(() => {}, 0)
The text was updated successfully, but these errors were encountered:
No branches or pull requests
Timer 属于 Dart 异步编程的一部分,通过 Timer 可以创建执行定时器(包括重复执行的)。
主要包含
Timer
和Timer.periodic
两个构造函数:分别对应 javascript 的
setTimeout
和setInterval
如 3 秒之后打印:
1 秒重复打印:
需要取消定时器,可以使用实例方法
.cancel
Dart 和 js 一样都是单线程,所以为了能够让定时器尽快的调用,可以使用静态方法
run
这类似于将 setTimeout 的时间设置为 0
The text was updated successfully, but these errors were encountered: