-
-
Notifications
You must be signed in to change notification settings - Fork 15
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Best way to run something every N seconds, precisely #10
Comments
|
Thank you for your prompt response! Unfortunately this solution has the issue that it slowly drifts forwards. For N=5s and with a start at 00, it will log at 00, 05, 10, ... Do you know of any solution to this? |
The better approach would be to use |
Edit: Not #9, I meant look at local time support in breejs/bree#76 and https://github.com/breejs/issues/74 |
The drifting is most likely because of code execution time, perhaps there can be optimizations done in our source code further in |
Unfortunately from what I understand in CRON the same issue with values such as "every 45 seconds" occurs. Using step values (/45) causes CRON to run at 00:00 and 00:45, but not at 01:30.
Indeed. This is the same behaviour of using chained setTimeouts (first example here: https://javascript.info/settimeout-setinterval#nested-settimeout). However for our usecase this is not acceptable. We need to run every N seconds, regardless of how long the task takes, not simply wait 10 seconds between executions. This is well explained in the link above. |
Could you review our source code to check for places we could optimize to ensure accuracy? |
It's not a matter of code efficiency. The issue is that
As you can see, it slowly drifts forward by a few ms every execution. After about 2-300 executions on my system, it will have drifted by a whole second (and now it executes on odd seconds instead of even seconds!). This is well documented by several issues and blogposts. E.g.:
One solution is to use recursive Here are a few packages addressing this: |
So in short this is two issues in one:
|
Thank you so much for this @adrfantini. Excellent writeup. By chance are you interested in crafting a pull request to address this? |
TBH I'm not really sure where to start (and I am a complete JS noob). |
Hi!
I am looking to schedule a task with
bree
every N seconds. N is chosen by the user.Let's say N=45s. This means the function should be called e.g. at 13:00:00,13:00:45, 13:01:30, 13:02:15, 13:03:00, ...
Of course this becomes more complex for other Ns, such as N=167.
Ideally I could do it like this:
However, this does not actually run every 45 seconds, but it actually triggers every x:00 and x:45.
Is there any way to schedule something like this?
The text was updated successfully, but these errors were encountered: