<!-- https://abap2ui5.github.io/docs/cookbook/browser_interaction/timer -->

# Timer

Fire a backend event after a delay with the `start_timer` frontend event. Handy for dashboards, status monitors, or any case that needs a delayed or periodic update without user interaction.

Pass the event name to fire and the delay in milliseconds:

```abap
client->follow_up_action(
    val   = client->cs_event-start_timer
    t_arg = VALUE #( ( `REFRESH` ) ( `2000` ) ) ).
```

After 2 seconds the browser triggers a backend roundtrip with the event name `REFRESH`, which you handle via `check_on_event` like any other event.

## Periodic Refresh

To get a repeating timer, simply re-arm it at the end of each handler:

```abap
CLASS z2ui5_cl_sample_timer DEFINITION PUBLIC.

  PUBLIC SECTION.
    INTERFACES z2ui5_if_app.
    DATA counter TYPE i.

  PROTECTED SECTION.
  PRIVATE SECTION.
ENDCLASS.

CLASS z2ui5_cl_sample_timer IMPLEMENTATION.
  METHOD z2ui5_if_app~main.

    CASE abap_true.

      WHEN client->check_on_init( ).
        DATA(view) = z2ui5_cl_ui5_view_builder=>factory(
            )->ele( n = `View` ns = `mvc`
                )->a( n = `xmlns`     v = `sap.m`
                )->a( n = `xmlns:mvc` v = `sap.ui.core.mvc`

                )->ele( `Page`
                    )->a( n = `title` v = `abap2UI5 - Timer`

                    )->tag( `Text`
                        )->a( n = `text` v = client->_bind( counter ) ).

        client->view_display( view->stringify( ) ).

        client->follow_up_action( val   = client->cs_event-start_timer
                        t_arg = VALUE #( ( `TICK` ) ( `2000` ) ) ).

      WHEN client->check_on_event( `TICK` ).
        counter = counter + 1.
        client->follow_up_action( val   = client->cs_event-start_timer
                        t_arg = VALUE #( ( `TICK` ) ( `2000` ) ) ).

    ENDCASE.

  ENDMETHOD.
ENDCLASS.
```

The counter increments every 2 seconds. To stop the loop, simply don't re-arm the timer in the handler.

## One-Shot Timer

A single `start_timer` call fires once — perfect for a deferred action like opening a new tab after a short delay:

```abap
WHEN client->check_on_event( `BUTTON_OPEN_NEW_TAB` ).
  client->follow_up_action( val   = client->cs_event-start_timer
                  t_arg = VALUE #( ( `FIRE_OPEN_TAB` ) ( `500` ) ) ).

WHEN client->check_on_event( `FIRE_OPEN_TAB` ).
  client->follow_up_action( val   = client->cs_event-open_new_tab
                  t_arg = VALUE #( ( `https://www.google.com/search?q=abap2ui5` ) ) ).
```

## Replacing a Pending Timer

There is one timer at a time. Calling `start_timer` again before the previous one fires replaces it — useful for a debounce, e.g. auto-saving an input field 500 ms after the last keystroke.

::: warning
Each timer tick causes a full backend roundtrip. Use sensible intervals (e.g. 2000 ms or more) to avoid heavy server load.
:::

<!-- samples:start (generated by scripts/link-samples.mjs — do not edit) -->

## Working Samples

Complete apps from the [sample catalog](https://github.com/abap2UI5/samples/blob/main/SAMPLES.md)
that use what this page describes. Each is a single class — pull the repository with
[abapGit](https://abapgit.org) and start it with `?app_start=<class>`.

| Sample | Class |
|---|---|
| Progress Indicator during a Backend Call (A) | [`Z2UI5_CL_SMP_APP_064`](https://github.com/abap2UI5/samples/blob/main/src/01/z2ui5_cl_smp_app_064.clas.abap) |
| Refresh the View Every n Seconds (A) | [`Z2UI5_CL_SMP_APP_028`](https://github.com/abap2UI5/samples/blob/main/src/01/z2ui5_cl_smp_app_028.clas.abap) |

<!-- samples:end -->
