如何在嵌套的定时器内取消子定时器?

huangapple go评论55阅读模式
英文:

How to cancel sub Timer inside nested Timer?

问题

以下是您要翻译的部分:

I am having a problem with cancelling one nested Timer. These are the code example I have:

import 'dart:async';

void main() async{
 final timer =
    Timer(const Duration(seconds: 1), () {
      print('Timer 1');
      Timer(const Duration(seconds:1),(){
        print('Timer 2');
      });
    });
  
  Timer(Duration(milliseconds: 1500),(){
    timer.cancel();    
        print('timer cancelled');
      });
}

The result:

Timer 1
timer cancelled
Timer 2

What I am expected:

Timer 1
timer cancelled

A little about my usecase, I want to create a quite complex animation and I use the nested Timer to set the specific timing of the animation.

The problem occur when the user move forward and instantinously move backward, the animation that still inside the Timer will still run 'forward' (because it's still deep inside the nested timer) even though the 'reverse' animation should be the only one that run.

That is why I am only expecting Timer 1 to be printed instead of both Timer 1 and 2 even though the Timer has been cancelled

Any feedback or input would be appreciated.

What I am expected:

Timer 1
timer cancelled
英文:

I am having a problem with cancelling one nested Timer. These are the code example I have:

import 'dart:async';

void main() async{
 final timer =
    Timer(const Duration(seconds: 1), () {
      print('Timer 1');
      Timer(const Duration(seconds:1),(){
        print('Timer 2');
      });
    });
  
  Timer(Duration(milliseconds: 1500),(){
    timer.cancel();    
        print('timer cancelled');
      });
}

The result:

Timer 1
timer cancelled
Timer 2

What I am expected:

Timer 1
timer cancelled

A little about my usecase, I want to create a quite complex animation and I use the nested Timer to set the specific timing of the animation.

The problem occur when the user move forward and instantinously move backward, the animation that still inside the Timer will still run 'forward' (because it's still deep inside the nested timer) even though the 'reverse' animation should be the only one that run.

That is why I am only expecting Timer 1 to be printed instead of both Timer 1 and 2 even though the Timer has been cancelled

Any feedback or input would be appreciated.

What I am expected:

Timer 1
timer cancelled

答案1

得分: -1

我认为这将打印出您期望的结果。只需保留对第二个Timer的引用。您可以重用现有的timer变量,因为在第一个Timer触发后创建第二个Timer后,您不再需要对第一个Timer的引用。

import 'dart:async';

void main(List<String> args) {
  Timer? timer = null;
  timer = Timer(const Duration(seconds: 1), () {
    print('Timer 1');
    timer = Timer(const Duration(seconds: 1), () {
      print('Timer 2');
    });
  });

  Timer(Duration(milliseconds: 1500), () {
    timer?.cancel();
    print('timer cancelled');
  });
}
英文:

I think this will print your expected result. Just keep a reference to the second Timer. You can reuse the existing timer variable since after the first Timer fires to create the second Timer, you no longer need a reference to the first one.

import &#39;dart:async&#39;;

void main(List&lt;String&gt; args) {
  Timer? timer = null;
    timer = Timer(const Duration(seconds: 1), () {
      print(&#39;Timer 1&#39;);
      timer = Timer(const Duration(seconds: 1),(){
        print(&#39;Timer 2&#39;);
      });
    });
  
  Timer(Duration(milliseconds: 1500),(){
    timer?.cancel();    
        print(&#39;timer cancelled&#39;);
      });
}

huangapple
  • 本文由 发表于 2023年2月18日 12:06:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/75491113.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定