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

Reliable way of invoking a function every second

What would be the most reliable way to invoke a function every second, even if the screen is off? Just think of a timer or stopwatch and when time is up, the user should be informed by using a vibration pattern.

 

As the clock callback is not invoked when the screen is off, this doesn't seem a suitable solution. Sure I can disable the display.autoOff resulting in a lot of power consumption.

 

Using setInterval or setTimeout instead, facilitates for function calls even when the screen is off, but the intervals are not accurate. Depending on how much work is done in the called function the interval could differ up to 25% from the value set.

 

Regarding this, does 'new Date()' deliver reliable date, such as I can try to compensate a larger interval by a following smaller interval. But this would push for uneven updates of information on the screen, which is not what I want neither.

 

And to make things worse: When using the simulator, using setInterval / setTimeout works as expected invoking the function after the exact interval.

 

Any advice is appreciated.

Best Answer
15 REPLIES 15

Good question, I was wondering what is the best way to do an hourly chime with my watchface...

Best Answer
0 Votes

`setInterval()` when the display is off is bad for battery life. 

 

I wonder if calculating a wake-up time, then using setTimeout() would be best in both of these scenarios.

 

 

 

 

Best Answer
0 Votes

@JonFitbit What exactly do you mean by 'wake-up' and how would you do this? Does it need some system callbacks (e.g. screen off) then in order to redraw the screen's content (e.g. update some information that depict the total time passed?)

Best Answer
0 Votes

Something like this, untested:

 

 

import { display } from "display";

let timeoutID;
display.addEventListener("change", function() { if (display.on) { wake(); } else { sleep(); } }) function wake() { // do something when the screen wakes

// also, cancel the timeout if it's still scheduled
clearTimeout(timeoutID); } function sleep() {
// do something when the screen sleeps

// here you can calculate when you need to wake up

let delayMilliseconds = 30 * 60 * 1000; // 30 minutes
timeoutID = setTimeout(doSomething, delayMilliseconds); }

function doSomething() {
// do this even when the screen is still off
}

 

Best Answer
0 Votes

Well, I believe the other issue was the accuracy of setInteval/setTimeout. I will play with it, the hourly chime seems a great experiment to measure this.

Thanks for the code snippet.

Best Answer
0 Votes

Did you ever figure out a way to do a battery efficient wakeup every x time interval to run a function? I'm looking into this now to change text in a textbox every hour. 

Best Answer
0 Votes

Did you ever find away to do this?

Looking for a variable to be reset each hour even if the watch face is off. 

Best Answer
0 Votes

did you ever get the hourly chime to work?  

Best Answer
0 Votes

The following is similar to the technique @JonFitbit is discussing here. I have tested it and it does work quite well.

var stpTO
   ,mPH=3600000

function prcS(resetTmr=true){
  let crMs=(new Date()).getTime()   //current time in milliseconds
  let cHMs=Math.floor(crMs/mPH)*mPH //milliseconds at turn of current hour

// process here

  if (resetTmr){
    clearTimeout(stpTO)                   //Always clear timer variable
    stpTO=setTimeout(prcS,(cHMs+mPH)-crMs)//Set timer to next hour
  }
}

For my case, I am executing a function every hour that marks the last time it ran. If more than an hour has passed when the watch face is started up again, the function is executed immediately and then rescheduled for the next hour, on the hour. I think for the purpose of an hourly chime, this workaround would be unnecessary.

 

Regards,

Best Answer
0 Votes
Fantastic thank you I have been searching for this for quite a while I'm a
rookie
Best Answer
0 Votes
Caveat Emptor,... Executing a function every second will significantly affect battery life.

From my experience though, hourly doesn't appear to affect battery life in any meaningful way.
Best Answer
0 Votes
i put this at the beginning of my app/index.js (after "imports" etc.)
i am apparently still doing something wrong ...



var stpTO,mPH=3600000;


function prcS(resetTmr=true){
let crMs=(new Date()).getTime()
//current time in milliseconds
let cHMs=Math.floor(crMs/mPH)*mPH
//milliseconds at turn of current hour
// process here
vibration.start('ping')

if (resetTmr){
clearTimeout(stpTO)
//Always clear timer variable
stpTO=setTimeout(prcS,(cHMs+mPH)-crMs)
//Set timer to next hour
}
}
Best Answer
0 Votes

You have to execute it at least once.

var stpTO,mPH=3600000
function prcS(resetTmr=true){
  let crMs=(new Date()).getTime()
  let cHMs=Math.floor(crMs/mPH)*mPH

  vibration.start('ping')

  if (resetTmr){
    clearTimeout(stpTO)
    stpTO=setTimeout(prcS,(cHMs+mPH)-crMs)
  }
}
prcS()

 

Best Answer
0 Votes
so i could do it like this ??



var stpTO,mPH=3600000;

function prcS(resetTmr=true){
let crMs=(new Date()).getTime();
//current time in milliseconds
let cHMs=Math.floor(crMs/mPH)*mPH;
//milliseconds at turn of current hour
// process here
vibration.start('ping');

if (resetTmr){
clearTimeout(stpTO);
//Always clear timer variable
stpTO=setTimeout(prcS,(cHMs+mPH)-crMs);
//Set timer to next hour
}
}

clock.ontick = () => prcS();



i also noticed i was missing a bunch of ";" at ends of command lines.
seriously, maybe i am too old to start dabbling with this.
Best Answer
0 Votes

@dzawiskie wrote:
so i could do it like this ??
...
i also noticed i was missing a bunch of ";" at ends of command lines.
seriously, maybe i am too old to start dabbling with this.

I in fact do that in a similar way, triggering within the one second ontick function. This is why the timer variable needs to be cleared, in case one is already in progress.

 

You will need to check if the hour has changed, otherwise you will chime every time the function is executed (~once per second).

 

As far as the other thing goes,... javaScript is very forgiving; use the semi colon if it helps, or leave it off. It is useful when reducing multiple commands to a single line for readability, but otherwise redundant.

 

Just be aware the line return is treated as a semi colon. The following returns nothing, and not a+b

function test(a,b){
  return
  a + b;
}

 

 Regards,

Best Answer
0 Votes