flutter 从移动端开始

flutter 定时器

2019-10-22  本文已影响0人  super_chao
回调一次的定时器
Timer(Duration(seconds: 1), () {
 //到时回调
 print('afterTimer='+DateTime.now().toString());
});
周期性定时器(例如获取验证码)
//回调周期
const period = const Duration(seconds: 1);
Timer.periodic(period, (timer) {
 //到时回调
 print('afterTimer='+DateTime.now().toString());
 count++;
 if (count >= 5) { // 需要手动取消
  //取消定时器,避免无限回调
  timer.cancel();
  timer = null;
 }
});

注意

周期的可以内部取消,当然也可以外部取消

 @override
  void dispose() {
    _timer?.cancel();
    _timer = null;
    super.dispose();
  }
上一篇 下一篇

猜你喜欢

热点阅读