|
| 1 | +# Timer, Auto-Refresh |
| 2 | + |
| 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. |
| 4 | + |
| 5 | +#### Basic Usage |
| 6 | + |
| 7 | +Use the `client->timer_set` method to schedule a backend event after a given interval in milliseconds: |
| 8 | + |
| 9 | +```abap |
| 10 | +CLASS z2ui5_cl_sample_timer DEFINITION PUBLIC FINAL CREATE PUBLIC. |
| 11 | +
|
| 12 | + PUBLIC SECTION. |
| 13 | + INTERFACES z2ui5_if_app. |
| 14 | + DATA counter TYPE i. |
| 15 | +
|
| 16 | +ENDCLASS. |
| 17 | +
|
| 18 | +CLASS z2ui5_cl_sample_timer IMPLEMENTATION. |
| 19 | + METHOD z2ui5_if_app~main. |
| 20 | +
|
| 21 | + CASE abap_true. |
| 22 | +
|
| 23 | + WHEN client->check_on_init( ). |
| 24 | + client->view_display( z2ui5_cl_xml_view=>factory( |
| 25 | + )->page( `abap2UI5 - Timer` |
| 26 | + )->text( client->_bind( counter ) |
| 27 | + )->stringify( ) ). |
| 28 | +
|
| 29 | + client->timer_set( |
| 30 | + interval_ms = 2000 |
| 31 | + event_finished = `TIMER_FINISHED` ). |
| 32 | +
|
| 33 | + WHEN client->check_on_event( `TIMER_FINISHED` ). |
| 34 | + counter = counter + 1. |
| 35 | + client->view_model_update( ). |
| 36 | +
|
| 37 | + "set timer again for continuous refresh |
| 38 | + client->timer_set( |
| 39 | + interval_ms = 2000 |
| 40 | + event_finished = `TIMER_FINISHED` ). |
| 41 | +
|
| 42 | + ENDCASE. |
| 43 | +
|
| 44 | + ENDMETHOD. |
| 45 | +ENDCLASS. |
| 46 | +``` |
| 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. |
| 48 | + |
| 49 | +#### Dashboard Auto-Refresh |
| 50 | + |
| 51 | +A common use case is refreshing dashboard data periodically. The following example fetches updated data from the database on every timer event: |
| 52 | + |
| 53 | +```abap |
| 54 | +CLASS z2ui5_cl_sample_dashboard DEFINITION PUBLIC FINAL CREATE PUBLIC. |
| 55 | +
|
| 56 | + PUBLIC SECTION. |
| 57 | + INTERFACES z2ui5_if_app. |
| 58 | + DATA last_refresh TYPE string. |
| 59 | + DATA mt_orders TYPE STANDARD TABLE OF z2ui5_t_draft WITH EMPTY KEY. |
| 60 | +
|
| 61 | +ENDCLASS. |
| 62 | +
|
| 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 }|. |
| 70 | +
|
| 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 ) ). |
| 77 | +
|
| 78 | + tab->columns( |
| 79 | + )->column( )->text( `ID` )->get_parent( |
| 80 | + )->column( )->text( `Status` ). |
| 81 | +
|
| 82 | + tab->items( )->column_list_item( )->cells( |
| 83 | + )->text( `{UUID}` |
| 84 | + )->text( `{UUID_PREV}` ). |
| 85 | +
|
| 86 | + client->view_display( tab->stringify( ) ). |
| 87 | +
|
| 88 | + client->timer_set( |
| 89 | + interval_ms = 5000 |
| 90 | + event_finished = `REFRESH` ). |
| 91 | +
|
| 92 | + WHEN client->check_on_event( `REFRESH` ). |
| 93 | + "refresh data |
| 94 | + last_refresh = |{ sy-datum DATE = USER } { sy-uzeit TIME = USER }|. |
| 95 | + client->view_model_update( ). |
| 96 | +
|
| 97 | + client->timer_set( |
| 98 | + interval_ms = 5000 |
| 99 | + event_finished = `REFRESH` ). |
| 100 | +
|
| 101 | + ENDCASE. |
| 102 | +
|
| 103 | + ENDMETHOD. |
| 104 | +ENDCLASS. |
| 105 | +``` |
| 106 | + |
| 107 | +#### Stopping the Timer |
| 108 | + |
| 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: |
| 110 | + |
| 111 | +```abap |
| 112 | + WHEN client->check_on_event( `TOGGLE_TIMER` ). |
| 113 | + IF auto_refresh = abap_true. |
| 114 | + auto_refresh = abap_false. |
| 115 | + 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` ). |
| 129 | + ENDIF. |
| 130 | + client->view_model_update( ). |
| 131 | +``` |
| 132 | + |
| 133 | +::: warning |
| 134 | +Keep in mind that each timer event triggers a full backend roundtrip. Use reasonable intervals (e.g., 2000ms or higher) to avoid excessive server load. |
| 135 | +::: |
0 commit comments