**every 5 seconds something will be called**
Timer.periodic(Duration(seconds: 5), (timer) {
//something();
});
**every 1 minute something() will be called.**
var cron = new Cron();
cron.schedule(new Schedule.parse('*/1 * * * *'), () async {
// something();
});
but both don't execute the job if is the app no longer in the memory.
The advantage of Cron over Timer is that with Cron Syntax you can specify complex time intervals and not only constant duration intervals.
For example it's difficult to execute a function every day at a specific time (e.g 6:05pm) with Timer because it depends on your start time to which it adds a constant duration, not like Cron you can just put:
var cron = new Cron();
cron.schedule(new Schedule.parse('5 6 * * *'), () async {
// something();
});
To sum up, if your goal is only to repeat a task over a constant interval use Timer, if you goal is more complex like repeating a task every 2 months in weekdays at 6pm you may need Cron.
var cron = new Cron();
cron.schedule(new Schedule.parse('0 6 * */2 MON-FRI'), () async {
// something();
});
Here is a link to know more about Cron syntax : corn syntax
Related
I am new to periodic background sync, What I want to achieve is that my website should ping to the server at 5 hours of interval by doing so It will receive data from server which will then be processed. I wanted to know if it's possible to set minInterval in periodic background sync to 5 hours or we can't. If their is another method to achieve this can you please give me some source or even an code example might be good.
In this following example of MDN they have set it to one day. Can I reduce it to 5 hours.?
async function registerPeriodicNewsCheck() {
const registration = await navigator.serviceWorker.ready;
try {
await registration.periodicSync.register('get-latest-news', {
minInterval: 24 * 60 * 60 * 1000,
});
} catch {
console.log('Periodic Sync could not be registered!');
}
}
Thanks for your help.
I'm looking to call a function at 10am, 10.05am, 10.10am, 10.15am....
I tried using
Timer.periodic(Duration(minutes: 5), (Timer t) { function...})
But it doesn't call the function at 10am, 10.05am... If I were to start the timer at 10.02am, it will call the function at 10.07am, 10.12am. Any idea how to workaround this? Thanks in advance!
I found a workaround where i use a timer to trigger a periodic timer
Rough code as shown below:
Timer(nextFiveMinInterval.difference(DateTime.now()),
() { Timer.periodic(Duration(minutes: 5), (Timer t) { function...})
})
I am trying to get a switch widget to turn off at a specific time of the day.
I have read a lot of the documentations like these
https://stackoverflow.com/questions/15848214/does-dart-have-a-scheduler
https://pub.dev/packages/cron
https://pub.dev/packages/scheduled_timer
All of these which can only allow me to set duration instead of a specific time.
Thus, my only approach right now is by setting up the timer when the switch is turned on.
e.g. 8hrs then it turns off.
Problem: If the user turned on the switch late, the time that it turns off will also be delayed.
So is there an actual way to set an event at a specific time + works even after we onstop/terminate the application?
You can try to do something like this:
I'll simplify the specific time into :
...
var setTime = DateTime.utc(2022, 7, 11, 8, 48, 0).toLocal();
StreamSubscription? subscription;
...
Then you can assign a periodic stream listener:
...
// periodic to run every second (you can change to minutes/hours/others too)
var stream = Stream.periodic(const Duration(seconds: 1), (count) {
//return true if the time now is after set time
return DateTime.now().isAfter(setTime);
});
//stream subscription
subscription = stream.listen((result) {
// if true, insert function and cancel listen subscription
if(result){
print('turn off');
subscription!.cancel();
}
// else if not yet, run this function
else {
print(result);
}
});
...
However, running a Dart code in a background process is more difficult, here are some references you can try:
https://medium.com/flutter/executing-dart-in-the-background-with-flutter-plugins-and-geofencing-2b3e40a1a124
https://pub.dev/packages/flutter_background_service
I hope it helps, feel free to comment if it doesn't work, I'll try my best to help.
After some time I figured it out.
Format
cron.schedule(Schedule.parse('00 00 * * *'), () async {
print("This code runs at 12am everyday")
});
More Examples
cron.schedule(Schedule.parse('15 * * * *'), () async {
print("This code runs every 15 minutes")
});
To customize a scheduler for your project, read this
I have this simple send mail function in Flutter, and I would like it to be executed (sent) for example every 48 hours. How would I go around doing that? Is there a simple way to time when it is executed? I don't think code is necessary here, but let me know if you need my send mail function (it is regular Mailer function).
You could use the Timer class:
const everySecondDay = const Duration(hours: 48);
final timer = Timer.periodic(everySecondDay, (Timer t) => sendMailFunction());
Then cancel it when appropriate:
timer.cancel();
I am using multiple Timelines in my application as
Timeline timeLine1 = new Timeline(new KeyFrame(Duration.seconds(1), actionEvent -> {
System.out.println("Enter in timer for check and playcontent");
MaintainPlaybackArray.checkAndPlayContent();
}));
timeLine1.setCycleCount(Timeline.INDEFINITE);
timeLine1.play();
Which will play each seconds
Timeline timeLine2 = new Timeline(new KeyFrame(Duration.seconds(5), actionEvent -> {
System.out.println("Enter in timer");
getSchedule();
checkSchedule();
}));
System.out.println("after timer");
timeLine2.setCycleCount(Timeline.INDEFINITE);
timeLine2.play();
Which Calls every 5 Seconds
Timeline timeLine3 = new Timeline(new KeyFrame(Duration.seconds(10), actionEvent -> {
System.out.println("in timer");
String currentChecksum = Util.md5(getScheduleJsonArray().toJSONString());
if (currentChecksum != null && !currentChecksum.equalsIgnoreCase(checksum)) {
System.out.println("Schedule changed");
isScheduleChanged = true;
checksum = currentChecksum;
}
}));
System.out.println("after timer");
timeLine3.setCycleCount(Timeline.INDEFINITE);
timeLine3.play();
which will executes after each 10 second.
Problem
When i execute the application after starting all the timelines it cannot play in synchronous manner.
i.e:
timLine1 will not executes for 8 seconds and then suddenly it executes 8 times at a time.
timeline2 will execute any time like after 8 seconds or twice after 12 seconds.
timeLine3 will executes after 12 seconds or 15 seconds.
So, Please help so that i can execute the code in synchronous manner.
is there anything i can use instead of Timeline?
Use ParallelTransition or SequentialTransition depending on your needs.