【问题标题】:How do i cancel a specific timer?如何取消特定的计时器?
【发布时间】:2020-10-08 12:39:04
【问题描述】:

如果我为 3 个不同的功能运行 3 个不同的计时器,我该如何取消特定的计时器。

例子

const oneSec = const Duration(seconds: 60);
        new Timer.periodic(
            oneSec,
            (Timer t) async =>
                await appLoggerRepository.putAppLogOnServer(jobName));

const oneSec = const Duration(seconds: 60);
        new Timer.periodic(
            oneSec,
            (Timer t) async =>
                await appLoggerRepository.uploadOrder(jobName));

const oneSec = const Duration(seconds: 60);
        new Timer.periodic(
            oneSec,
            (Timer t) async =>
                await appLoggerRepository.checkdevice(jobName));

~~

【问题讨论】:

    标签: flutter dart timer cancellation periodic-task


    【解决方案1】:

    你必须创建一个计时器变量

    Timer logTimer = Timer.periodic( //no need for the new keyword
                oneSec,
                (Timer t) async =>
                    await appLoggerRepository.putAppLogOnServer(jobName));
    
    //to cancel call cancel()
    logTimer.cancel();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-01-08
      相关资源
      最近更新 更多