11# Timer, Auto-Refresh
22
3- abap2UI5 provides a timer functionality that triggers backend events after a specified interval. This is useful for dashboards, status monitors, or any scenario that requires periodic data updates without manual user interaction.
3+ abap2UI5 provides a custom control ` z2ui5.Timer ` that triggers events after a specified delay. This is useful for dashboards, status monitors, or any scenario that requires periodic data updates without manual user interaction.
4+
5+ The timer is added as a view element via ` _z2ui5( )->timer( ... ) ` and supports the following parameters:
6+
7+ | Parameter | Description |
8+ | ---------------| --------------------------------------------------|
9+ | ` finished ` | Event raised when the timer fires |
10+ | ` delayms ` | Delay in milliseconds before firing |
11+ | ` checkactive ` | Bind to an ` abap_bool ` to activate/deactivate |
12+ | ` checkrepeat ` | If ` abap_true ` , the timer repeats automatically |
413
514#### Basic Usage
615
7- Use the ` client->timer_set ` method to schedule a backend event after a given interval in milliseconds :
16+ Embed the timer in your view and control it via data binding. The following example increments a counter every 2 seconds :
817
918``` abap
10- CLASS z2ui5_cl_sample_timer DEFINITION PUBLIC FINAL CREATE PUBLIC.
19+ CLASS z2ui5_cl_sample_timer DEFINITION PUBLIC CREATE PUBLIC.
1120
1221 PUBLIC SECTION.
1322 INTERFACES z2ui5_if_app.
1423 DATA counter TYPE i.
24+ DATA check_timer_active TYPE abap_bool.
1525
1626ENDCLASS.
1727
@@ -21,111 +31,106 @@ CLASS z2ui5_cl_sample_timer IMPLEMENTATION.
2131 CASE abap_true.
2232
2333 WHEN client->check_on_init( ).
34+ check_timer_active = abap_true.
35+
2436 client->view_display( z2ui5_cl_xml_view=>factory(
2537 )->page( `abap2UI5 - Timer`
2638 )->text( client->_bind( counter )
39+ )->_z2ui5( )->timer(
40+ checkactive = client->_bind( check_timer_active )
41+ delayms = `2000`
42+ finished = client->_event( `TIMER_FINISHED` )
43+ checkrepeat = abap_true
2744 )->stringify( ) ).
2845
29- client->timer_set(
30- interval_ms = 2000
31- event_finished = `TIMER_FINISHED` ).
32-
3346 WHEN client->check_on_event( `TIMER_FINISHED` ).
3447 counter = counter + 1.
3548 client->view_model_update( ).
3649
37- "set timer again for continuous refresh
38- client->timer_set(
39- interval_ms = 2000
40- event_finished = `TIMER_FINISHED` ).
41-
4250 ENDCASE.
4351
4452 ENDMETHOD.
4553ENDCLASS.
4654```
47- The timer fires once after the interval expires. To create a continuous auto-refresh loop, simply re-set the timer inside the event handler .
55+ With ` checkrepeat = abap_true ` , the timer restarts automatically after each event — no need to re-trigger it manually .
4856
49- #### Dashboard Auto-Refresh
57+ #### One-Shot Timer
5058
51- A common use case is refreshing dashboard data periodically. The following example fetches updated data from the database on every timer event :
59+ If you only need the timer to fire once (e.g., to open a new browser tab), omit ` checkrepeat ` and control activation via ` checkactive ` :
5260
5361``` abap
54- CLASS z2ui5_cl_sample_dashboard DEFINITION PUBLIC FINAL CREATE PUBLIC.
62+ CLASS z2ui5_cl_sample_timer_once DEFINITION PUBLIC CREATE PUBLIC.
5563
5664 PUBLIC SECTION.
5765 INTERFACES z2ui5_if_app.
58- DATA last_refresh TYPE string.
59- DATA mt_orders TYPE STANDARD TABLE OF z2ui5_t_draft WITH EMPTY KEY.
66+ DATA mv_url TYPE string.
67+ DATA mv_check_timer_active TYPE abap_bool.
68+ DATA client TYPE REF TO z2ui5_if_client.
69+ METHODS display_view.
6070
6171ENDCLASS.
6272
63- CLASS z2ui5_cl_sample_dashboard IMPLEMENTATION.
64- METHOD z2ui5_if_app~main.
65-
66- CASE abap_true.
67-
68- WHEN client->check_on_init( ).
69- last_refresh = |{ sy-datum DATE = USER } { sy-uzeit TIME = USER }|.
73+ CLASS z2ui5_cl_sample_timer_once IMPLEMENTATION.
74+
75+ METHOD display_view.
76+
77+ DATA(view) = z2ui5_cl_xml_view=>factory( ).
78+
79+ client->view_display( view->shell(
80+ )->page( title = `abap2UI5 - Timer One-Shot`
81+ navbuttonpress = client->_event( `BACK` )
82+ shownavbutton = client->check_app_prev_stack( )
83+ )->_z2ui5( )->timer(
84+ checkactive = client->_bind( mv_check_timer_active )
85+ finished = client->_event_client(
86+ val = client->cs_event-open_new_tab
87+ t_arg = VALUE #( ( `$` && client->_bind( mv_url ) ) ) )
88+ )->simple_form( title = `Form Title`
89+ editable = abap_true
90+ )->content( `form`
91+ )->button(
92+ text = `open new tab`
93+ press = client->_event( `BUTTON_OPEN_NEW_TAB` )
94+ )->stringify( ) ).
7095
71- DATA(tab) = z2ui5_cl_xml_view=>factory(
72- )->page( `Dashboard`
73- )->header_content(
74- )->text( client->_bind( last_refresh )
75- )->get_parent(
76- )->table( client->_bind( mt_orders ) ).
96+ ENDMETHOD.
7797
78- tab->columns(
79- )->column( )->text( `ID` )->get_parent(
80- )->column( )->text( `Status` ).
98+ METHOD z2ui5_if_app~main.
8199
82- tab->items( )->column_list_item( )->cells(
83- )->text( `{UUID}`
84- )->text( `{UUID_PREV}` ).
100+ me->client = client.
85101
86- client->view_display( tab->stringify( ) ).
102+ IF client->check_on_init( ).
103+ mv_check_timer_active = abap_false.
104+ display_view( ).
105+ ENDIF.
87106
88- client->timer_set(
89- interval_ms = 5000
90- event_finished = `REFRESH` ).
107+ CASE client->get( )-event.
91108
92- WHEN client->check_on_event( `REFRESH` ) .
93- "refresh data
94- last_refresh = |{ sy-datum DATE = USER } { sy-uzeit TIME = USER }| .
109+ WHEN `BUTTON_OPEN_NEW_TAB` .
110+ mv_check_timer_active = abap_true.
111+ mv_url = `https://www.google.com/search?q=abap2ui5` .
95112 client->view_model_update( ).
96113
97- client->timer_set(
98- interval_ms = 5000
99- event_finished = `REFRESH` ).
114+ WHEN `BACK`.
115+ client->nav_app_leave( client->get_app( client->get( )-s_draft-id_prev_app_stack ) ).
100116
101117 ENDCASE.
102118
103119 ENDMETHOD.
104120ENDCLASS.
105121```
122+ Here the timer starts inactive. When the user clicks the button, ` mv_check_timer_active ` is set to ` abap_true ` and the timer fires once, opening a new browser tab via ` _event_client ` .
106123
107- #### Stopping the Timer
124+ #### Stopping a Repeating Timer
108125
109- To stop an active timer, simply do not call ` client->timer_set ` again in the event handler. You can also add a toggle button for user control :
126+ To stop a repeating timer, simply set the bound ` checkactive ` flag to ` abap_false ` :
110127
111128``` abap
112129 WHEN client->check_on_event( `TOGGLE_TIMER` ).
113- IF auto_refresh = abap_true.
114- auto_refresh = abap_false.
130+ IF check_timer_active = abap_true.
131+ check_timer_active = abap_false.
115132 ELSE.
116- auto_refresh = abap_true.
117- client->timer_set(
118- interval_ms = 2000
119- event_finished = `TIMER_FINISHED` ).
120- ENDIF.
121- client->view_model_update( ).
122-
123- WHEN client->check_on_event( `TIMER_FINISHED` ).
124- counter = counter + 1.
125- IF auto_refresh = abap_true.
126- client->timer_set(
127- interval_ms = 2000
128- event_finished = `TIMER_FINISHED` ).
133+ check_timer_active = abap_true.
129134 ENDIF.
130135 client->view_model_update( ).
131136```
0 commit comments