Dart/flutter how to change the timer inside periodic after every time

旧巷老猫 提交于 2020-06-17 12:55:20

问题


I'm making a custom logic for showing ads in my app, so when the user opens the app after 1 min pops up an ad then supposed this 1 min to get increased by 1 min every time, So x be 2 then 3 then 4 ...etc.

I tried to do so, but didn't work for me ( showing the ad every 1 min not increase it );

int x = 1;
Timer.periodic(Duration(minutes: x), (timer) { 
  x = x + 1;
  interstitialAd.load();
});

回答1:


I think you cannot do it. You can alternatively cancel the current timer and start a new timer based on conditions. Check the following pseudo-code

  Timer.periodic(Duration(minutes: 5), (timer) {
      if(condition){
        timer.cancel();
        createNewPeriodicTask();
      }
      // task code
  });


来源:https://stackoverflow.com/questions/62183435/dart-flutter-how-to-change-the-timer-inside-periodic-after-every-time

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!