Watch timer Class: Toybox::Timer::Timer

Former Member
Former Member
Can someone show me the format/example for using:

(Object) start(callback, time, repeat)

I want to update the UI once every 60 seconds, using this in an app and not a watch face

https://developer.garmin.com/downloads/connect-iq/monkey-c/doc/Toybox/Timer/Timer.html#start-instance_method

Thanks!
  • The Timer class is pretty simple. If you just want to force the UI to update every 60 seconds no matter what, you'd probably want to put the following code into your AppBase derived class.

    class MyApp extends App.AppBase
    {
    function initialize() {
    AppBase.initialize();
    }

    hidden var _M_timer;

    function onStart(params) {
    _M_timer = new Timer.Timer();
    _M_timer.start(self.method(:onTimer), 60000, true);
    }

    function onStop(params) {
    _M_timer.stop();
    _M_timer = null;
    }

    function onTimer() {
    Ui.requestUpdate();
    }
    }


    If you don't want to have the helper function onTimer(), then you can change the start() call to be something like this...

    _M_timer.start(new Lang.Method(Ui, :requestUpdate), 60000, true);


    It sounds like you are trying to implement watch-face like behavior in a watch-app. If so, you might take a peek at the code I posted here which does exactly that.

    Travis
  • could you please guide me when the timer should be set? On initialize or onLayout? 

    To make sure it is always started just once and not repetitively (e.g. when the watch face disappears and appears again)? 

  • I always do it in initialize() for widgets and device apps.

    Watchfaces are a different story as they can only run when the WF isn't in low power mode, so you want to use onEnterSleep() and onExitSleep() to control them.

  • How will they affect it? That a timer in low power might be ignored? Will it run after the watch leaves the low power mode?

    I just want to set a timer that hourly updates a location twice a day sunrise and sunset. But no problem if it will be a little later. It should just not skip completely.

    Is to set it in initialize for such use case ok?

  • I believe the app will crash if yo try to use a timer in low power mode.

    Sounds to me like you want to use a Background service that runs at the times you want, but even there, on a watchface, you can't start GPS.

  • I use background service for data loading and it would be too complex for such a simple task. It already hits memory limits, so it would need to iterate through 3 background tasks. And it needs to work for devices without the background service as well. 

    They actually suggest that the Timer should be used for updating position: https://developer.garmin.com/connect-iq/api-docs/Toybox/Position.html#getInfo-instance_function

    If it crashed in low-power mode, it would be completely useless, as you can not control it. 

    I think the timer might rather be skipped than crash. Great would be if it runs once back from low-power though. 

  • That's something you'd do in a watch app or widget.  Timers are really restricted in a WF.

  • Great would be if it runs once back from low-power

    Again, look at onEnterSleep and onExitSleep

    That's how you can see if you're in low or high power mode.

  • You are right. Not even possible to set the timer in the initialize of the watch face. So everybody who needs timers in the watch face basically needs to implement the same logic of pausing, resetting and calculating new time for the timer each time it fires or the watch wakes up. Lame.

  • Sounds like all you really need to do is something like keep track of the hour, and if it's been X hours, get the location.