Timers

You can calculate time using timers. They can measure time as well as trigger actions at set intervals or when the timer stops.

Timer properties

  • id: Timer ID. Required field.
  • duration: Timer period in milliseconds. If set to 0 or not set, the timer continues running indefinitely until the stop event is triggered.
  • tick_interval: Interval between timer ticks. The actual tick rate may differ from the specified value by a few milliseconds: we deliberately made it so that the variable stores the actual time that's not rounded up or down.
  • value_variable: Variable that stores the current timer value. It's updated every tick. This value reflects actual elapsed time, so it may not always be a multiple of tick_interval and may include additional milliseconds after the last tick.
  • tick_actions: Array of actions to execute on each timer tick.
  • end_actions: Array of actions to execute when the timer stops.

Timers are declared in the card object:

{
  "templates": {...},
  "card": {
    "timers": [
      {
        "id": "ticker",
        "duration": "@{timer_duration}",
        "tick_interval": "@{timer_interval}",
        "value_variable": "timer_value",
        "tick_actions": [],
        "end_actions": []
      }
    ],
    ...
  }
}

Controls

To control a timer from the layout, use the following actions:

Action

Description

div-action://timer?id=timer_id&action=start

Start the timer.

div-action://timer?id=timer_id&action=stop

Stop the timer and execute end_actions.

div-action://timer?id=timer_id&action=pause

Pause the timer.

div-action://timer?id=timer_id&action=resume

Resume the timer.

div-action://timer?id=timer_id&action=cancel

Interrupt the timer. value_variable doesn't change and retains its last saved value.

div-action://timer?id=timer_id&action=reset

Restart the timer (runs cancel, then start).

View an interactive example

Learn more

Follow DivKit news in the Telegram channel: http://t.me/divkit_news

You can also discuss topics of interest in the DivKit user community in Telegram: https://t.me/divkit_community_ru

DivKit Repository