proper way to change a timer while running?

I have a 1 minute timer running, and at times want to change it to 1 second, and then back to 1 minute (hint! think "low power mode" in a watch-app)

When I first create the timer, it's every minute, but based on a "gesture" (screen tap in this case), I want to call for "onTimer()" every second. This code in an a conditional that only is true when things change...)
if(showSeconds) {
timer.stop;
timer.start( method(:onTimer), 1000, true );
} else {
timer.stop;
timer.start( method(:onTimer), 1000*60, true );
}



However, I get a "too many timers" error in the simulator on the first switch! It "kind of" works if I don't do the timer.stop() (only errors out after two switches...)

Simply put, how do I change the "time" value in a timer?
  • Is it safe to assume that your code actually calls the function timer.stop()?
  • yes, doing the timer.stop() as shown in the code snip.

    Interesting thing is I threw together a basic app to demonstrate this, and it works fine. Time for some head scratching!
  • Ok, after a cup of coffee, I unserstand Travis! I was calling "timer.stop;" and not "timer.stop();"

    I'm surprised I didn't get a compile or a run time error the first time this was executed.
  • Technically, Jim's typo is valid syntax even though it doesn't do what he was expecting. :) I was discussing this with Kyle to see whether we should consider this a bug, but it's really not. The [FONT=courier new]timer[/FONT] object does have a [FONT=courier new]stop[/FONT], so at run time it finds it, adds it to the stack, and then goes on it's merry way without invoking the method.
  • Thanks Brandon, I understand why this type wasn't caught in compile or first pass execution, but what about the error I got when it was caught - "too many timers"? It wasn't really "too many timers", but was more like "starting a timer that is already running", or something like that..
  • Former Member
    Former Member over 9 years ago
    Hiya jim,

    I had lots of trouble with timers. I believe there is/was a limit of 3 instances, and that the same instance can be duplicated to reach that maximum. Anyways, with your timer is there a possibility that when the user switches back to minute updating, they might not be in sync with the minute interval? Thought I would add this, in case that is the case. The example will always update on the minute, but with some tweaks could be offset in updateMinutes to sync on the second that the user starts the timer.

    function stopViewTimer() {
    viewTimer.stop();
    isMinutes = false;
    isSeconds = false;
    }


    function updateMinutes() {
    if(!isMinutes) {
    viewTimer.stop();
    var sec = System.getClockTime().sec;
    var ms;
    if(0 == sec) {
    ms = 60000;
    }
    else {
    ms = 60000 - sec * 1000;
    }
    viewTimer.start(method(:updateSynced),ms,false);
    isMinutes = true;
    isSeconds = false;
    }
    Ui.requestUpdate();
    }


    function updateSeconds() {
    if(!isSeconds) {
    viewTimer.stop();
    viewTimer.start(method(:onViewTimer),1000,true);
    isSeconds = true;
    isMinutes = false;
    }
    Ui.requestUpdate();
    }


    function updateSynced() {
    viewTimer.stop();
    viewTimer.start(method(:onViewTimer),60000,true);
    Ui.requestUpdate();
    }


    function onViewTimer() {
    Ui.requestUpdate();
    }
  • shark - interesting! I hadn't considered syncing the minute timer to the actual change of minute. On the display, I'm basically just updating a clock, with an option to display the seconds for 15 seconds (like lowpower mode in a watchface). Do you know if in a watchface, the "low power" update is synced to the actual change of minute, or is it just "every minute"?
  • Former Member
    Former Member over 9 years ago
    shark - interesting! I hadn't considered syncing the minute timer to the actual change of minute. On the display, I'm basically just updating a clock, with an option to display the seconds for 15 seconds (like lowpower mode in a watchface). Do you know if in a watchface, the "low power" update is synced to the actual change of minute, or is it just "every minute"?
    Yeah. Without it, if you were to switch from second timing back to minute timing, it would take a full minute to see the next update, which means odds are that you would not see the minute change until too late. Re low power, no idea. I haven't gone back to update my faces yet, since low power was introduced.
  • sharkbait -Thanks for the info and example.. I now use a one shot time timer to get to the minute change, and then go into a repeating one minute timer, and it's working well!
  • Now if Ui.BehaviorDelegate and Ui.InputDelegate only had an "onGesture()" method so apps could use the same gesture used for watchfaces for entering and leaving low power mode as watchfaces do....

    Battery drain wise, I now have apps that drain the battery just like a CIQ watchface (very little drain), but can access GPS, sensors and comm when needed.., and allows for real and virtual keys, and alternate screens.