Cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Do an event once every 24 hours?

Is there a way that I can trigger a routine on the watch face one every 24 hours, the first time the user opens up the watch after midnight?  It only needs to be run once per day.

Best Answer
0 Votes
5 REPLIES 5

Yes. Keep track of the date at which the routine last ran, and compare the current date with that. When they're different, run the routine again and update the last-run date.

For efficiency, it might be best not to do this every second if you can conveniently avoid it.

Also be careful to consider what happens when your app starts.

Peter McLennan
Gondwana Software
Best Answer
0 Votes

@jaystarkey  Yon can use setInterval() to trigger an event in 24 hours time from a specific time.

Author | ch, passion for improvement.

Best Answer
0 Votes

This makes sense - what is the best way to store the date last ran value?  I'm new to development and not sure what persists/how to access persistent data.

Best Answer
0 Votes

Put it in app/index.js?

How would I get this to trigger only at 00:00 every day?

 

//run once every x hours
let numHours = 24;
setInterval(function(){ doStuff(); }, 1000 * 60 * 60 * numHours);

Best Answer

You can use the fs API to keep track of dateLastRun.

Be careful using setTimeout for this. Timers will be killed whenever the user starts another app (eg, Settings), so your special function may never run.

Also be careful about checking for an exact time. Your clockface might not be running at that exact time.

Peter McLennan
Gondwana Software
Best Answer
0 Votes