Reputation: 134
I have a periodic timer in my Flutter app, which changes some values on a page every x seconds:
periodicTimer = new Timer.periodic(Duration(seconds: timer), (Timer t) {
setState(() {
if (globals.selectedBottomNav == 0) {
nextKey();
} else {
nextCircleKey();
}
});
});
However, I need to be able to perform another action in a shorter time. So, for example, if my timer variable is set to 5 seconds, I need to be able to run my setState every 5 seconds, but after 3 seconds of each timed loop, I want to run some other code.
Any help on how I could achieve that? I've tried with various nested Timer and Timer.periodic options, but get strange results.
Thanks
Upvotes: 1
Views: 758
Reputation: 3067
I see nothing wrong with this simple implementation. If you are having unexpected behavior with this, please update your question with a detailed explanation of expected vs actual behavior, as well as any other solutions you have tried.
periodicTimer = new Timer.periodic(Duration(seconds: timer), (Timer t) {
Timer(Duration(seconds: 3), () {
// perform other operations here
});
setState(() {
if (globals.selectedBottomNav == 0) {
nextKey();
} else {
nextCircleKey();
}
});
});
Upvotes: 0
Reputation: 384
If you're set on having this done using a single timer, how about just shortening that timers duration and count the seconds yourself
int secondsCounter = 0;
periodicTimer = new Timer.periodic(Duration(seconds: 1), (Timer t) {
secondsCounter++;
if(secondsCounter == 3){
// do stuff that's supposed to happen after 3 seconds
} else if(secondsCounter == 5){
// do stuff that's supposed to happen after 5 seconds
}
if(secondsCounter >= 5){
secondsCounter = 0;
}
});
Upvotes: 1