1e41f4b71Sopenharmony_ci# Configuring a Widget to Update Periodically 2e41f4b71Sopenharmony_ci 3e41f4b71Sopenharmony_ciThe widget framework provides the following modes of updating widgets periodically: 4e41f4b71Sopenharmony_ci 5e41f4b71Sopenharmony_ci 6e41f4b71Sopenharmony_ci- Setting the update interval: The widget will be updated at the specified interval by calling [onUpdateForm](../reference/apis-form-kit/js-apis-app-form-formExtensionAbility.md#onupdateform). You can specify the interval by setting the [updateDuration](arkts-ui-widget-configuration.md) field in the **form_config.json** file. For example, you can configure the widget to update once an hour. 7e41f4b71Sopenharmony_ci 8e41f4b71Sopenharmony_ci > **NOTE** 9e41f4b71Sopenharmony_ci > 10e41f4b71Sopenharmony_ci > - Before configuring a widget to update periodically, enable the periodic update feature by setting the **updateEnabled** field to **true** in the **form_config.json** file. If both **updateDuration** and **scheduledUpdateTime** are set, the value specified by **updateDuration** is used. 11e41f4b71Sopenharmony_ci > 12e41f4b71Sopenharmony_ci > - To reduce the number of passive periodic update times and power consumption of widgets, the update interval can be set from the application market – for applications that are being or have been installed. 13e41f4b71Sopenharmony_ci > - If an update interval is set from the application market, it is compared with the value of **updateDuration** in the **form_config.json** file. Whichever longer is used. 14e41f4b71Sopenharmony_ci > - If no update interval is set from the application market, the value in the **form_config.json** file is used. 15e41f4b71Sopenharmony_ci > - This rule does not apply when the periodic update feature is disabled. 16e41f4b71Sopenharmony_ci > - The update interval set from the application market ranges from 1 to 336, in the unit of 30 minutes. That is, the minimum update interval is half an hour (1 x 30 minutes) and the maximum update interval is one week (336 x 30 minutes). 17e41f4b71Sopenharmony_ci > - This rule takes effect since API version 11. In earlier versions, the periodic update follows the settings in the **form_config.json** file. 18e41f4b71Sopenharmony_ci 19e41f4b71Sopenharmony_ci ```json 20e41f4b71Sopenharmony_ci { 21e41f4b71Sopenharmony_ci "forms": [ 22e41f4b71Sopenharmony_ci { 23e41f4b71Sopenharmony_ci "name": "UpdateDuration", 24e41f4b71Sopenharmony_ci "description": "$string:widget_updateduration_desc", 25e41f4b71Sopenharmony_ci "src": "./ets/updateduration/pages/UpdateDurationCard.ets", 26e41f4b71Sopenharmony_ci "uiSyntax": "arkts", 27e41f4b71Sopenharmony_ci "window": { 28e41f4b71Sopenharmony_ci "designWidth": 720, 29e41f4b71Sopenharmony_ci "autoDesignWidth": true 30e41f4b71Sopenharmony_ci }, 31e41f4b71Sopenharmony_ci "colorMode": "auto", 32e41f4b71Sopenharmony_ci "isDefault": true, 33e41f4b71Sopenharmony_ci "updateEnabled": true, 34e41f4b71Sopenharmony_ci "scheduledUpdateTime": "10:30", 35e41f4b71Sopenharmony_ci "updateDuration": 2, 36e41f4b71Sopenharmony_ci "defaultDimension": "2*2", 37e41f4b71Sopenharmony_ci "supportDimensions": [ 38e41f4b71Sopenharmony_ci "2*2" 39e41f4b71Sopenharmony_ci ] 40e41f4b71Sopenharmony_ci } 41e41f4b71Sopenharmony_ci ] 42e41f4b71Sopenharmony_ci } 43e41f4b71Sopenharmony_ci ``` 44e41f4b71Sopenharmony_ci 45e41f4b71Sopenharmony_ci- Setting the scheduled update time: The widget will be updated at the scheduled time every day. You can specify the time by setting the [scheduledUpdateTime](arkts-ui-widget-configuration.md) field in the **form_config.json** file. For example, you can configure the widget to update at 10:30 a.m. every day. 46e41f4b71Sopenharmony_ci 47e41f4b71Sopenharmony_ci > **NOTE** 48e41f4b71Sopenharmony_ci > 49e41f4b71Sopenharmony_ci > **updateDuration** takes precedence over **scheduledUpdateTime**. For the **scheduledUpdateTime** settings to take effect, set **updateDuration** to **0**. 50e41f4b71Sopenharmony_ci 51e41f4b71Sopenharmony_ci ```json 52e41f4b71Sopenharmony_ci { 53e41f4b71Sopenharmony_ci "forms": [ 54e41f4b71Sopenharmony_ci { 55e41f4b71Sopenharmony_ci "name": "ScheduledUpdateTime", 56e41f4b71Sopenharmony_ci "description": "$string:widget_scheupdatetime_desc", 57e41f4b71Sopenharmony_ci "src": "./ets/scheduledupdatetime/pages/ScheduledUpdateTimeCard.ets", 58e41f4b71Sopenharmony_ci "uiSyntax": "arkts", 59e41f4b71Sopenharmony_ci "window": { 60e41f4b71Sopenharmony_ci "designWidth": 720, 61e41f4b71Sopenharmony_ci "autoDesignWidth": true 62e41f4b71Sopenharmony_ci }, 63e41f4b71Sopenharmony_ci "colorMode": "auto", 64e41f4b71Sopenharmony_ci "isDefault": true, 65e41f4b71Sopenharmony_ci "updateEnabled": true, 66e41f4b71Sopenharmony_ci "scheduledUpdateTime": "10:30", 67e41f4b71Sopenharmony_ci "updateDuration": 0, 68e41f4b71Sopenharmony_ci "defaultDimension": "2*2", 69e41f4b71Sopenharmony_ci "supportDimensions": [ 70e41f4b71Sopenharmony_ci "2*2" 71e41f4b71Sopenharmony_ci ] 72e41f4b71Sopenharmony_ci } 73e41f4b71Sopenharmony_ci ] 74e41f4b71Sopenharmony_ci } 75e41f4b71Sopenharmony_ci ``` 76e41f4b71Sopenharmony_ci 77e41f4b71Sopenharmony_ci- Setting the next update time: The widget will be updated next time at the specified time. You can specify the time by calling the [setFormNextRefreshTime](../reference/apis-form-kit/js-apis-app-form-formProvider.md#setformnextrefreshtime) API. The minimum update interval is 5 minutes. For example, you can configure the widget to update within 5 minutes after the API is called. 78e41f4b71Sopenharmony_ci 79e41f4b71Sopenharmony_ci ```ts 80e41f4b71Sopenharmony_ci import { FormExtensionAbility, formProvider } from '@kit.FormKit'; 81e41f4b71Sopenharmony_ci import { hilog } from '@kit.PerformanceAnalysisKit'; 82e41f4b71Sopenharmony_ci import { BusinessError } from '@kit.BasicServicesKit'; 83e41f4b71Sopenharmony_ci 84e41f4b71Sopenharmony_ci const TAG: string = 'UpdateByTimeFormAbility'; 85e41f4b71Sopenharmony_ci const FIVE_MINUTE: number = 5; 86e41f4b71Sopenharmony_ci const DOMAIN_NUMBER: number = 0xFF00; 87e41f4b71Sopenharmony_ci 88e41f4b71Sopenharmony_ci export default class UpdateByTimeFormAbility extends FormExtensionAbility { 89e41f4b71Sopenharmony_ci onFormEvent(formId: string, message: string): void { 90e41f4b71Sopenharmony_ci // Called when a specified message event defined by the form provider is triggered. 91e41f4b71Sopenharmony_ci hilog.info(DOMAIN_NUMBER, TAG, `FormAbility onFormEvent, formId = ${formId}, message: ${JSON.stringify(message)}`); 92e41f4b71Sopenharmony_ci try { 93e41f4b71Sopenharmony_ci // Configure the widget to update in 5 minutes. 94e41f4b71Sopenharmony_ci formProvider.setFormNextRefreshTime(formId, FIVE_MINUTE, (err: BusinessError) => { 95e41f4b71Sopenharmony_ci if (err) { 96e41f4b71Sopenharmony_ci hilog.info(DOMAIN_NUMBER, TAG, `Failed to setFormNextRefreshTime. Code: ${err.code}, message: ${err.message}`); 97e41f4b71Sopenharmony_ci return; 98e41f4b71Sopenharmony_ci } else { 99e41f4b71Sopenharmony_ci hilog.info(DOMAIN_NUMBER, TAG, 'Succeeded in setFormNextRefreshTiming.'); 100e41f4b71Sopenharmony_ci } 101e41f4b71Sopenharmony_ci }); 102e41f4b71Sopenharmony_ci } catch (err) { 103e41f4b71Sopenharmony_ci hilog.info(DOMAIN_NUMBER, TAG, `Failed to setFormNextRefreshTime. Code: ${(err as BusinessError).code}, message: ${(err as BusinessError).message}`); 104e41f4b71Sopenharmony_ci } 105e41f4b71Sopenharmony_ci } 106e41f4b71Sopenharmony_ci ... 107e41f4b71Sopenharmony_ci } 108e41f4b71Sopenharmony_ci ``` 109e41f4b71Sopenharmony_ci 110e41f4b71Sopenharmony_ci 111e41f4b71Sopenharmony_ciWhen periodic update is triggered, the system calls the [onUpdateForm](../reference/apis-form-kit/js-apis-app-form-formExtensionAbility.md#onupdateform) lifecycle callback of the FormExtensionAbility. In the callback, [updateForm](../reference/apis-form-kit/js-apis-app-form-formProvider.md#updateform) can be used to update the widget. For details about how to use **onUpdateForm**, see [Updating Widget Content Through FormExtensionAbility](arkts-ui-widget-event-formextensionability.md). 112e41f4b71Sopenharmony_ci 113e41f4b71Sopenharmony_ci 114e41f4b71Sopenharmony_ci> **NOTE** 115e41f4b71Sopenharmony_ci> - Each widget can be updated at the specified interval for a maximum of 50 times every day, including updates triggered by setting [updateDuration](arkts-ui-widget-configuration.md) or calling [setFormNextRefreshTime](../reference/apis-form-kit/js-apis-app-form-formProvider.md#setformnextrefreshtime). When the limit is reached, the widget cannot be updated in this mode again. The number of update times is reset at 00:00 every day. 116e41f4b71Sopenharmony_ci> 117e41f4b71Sopenharmony_ci> - A single timer is used for timing updates at the specified interval. Therefore, if a widget is configured to update at scheduled intervals, the first scheduled update may have a maximum deviation of 30 minutes. For example, if widget A (updated every half an hour) is added at 03:20 and widget B (also updated every half an hour) is added at 03:40, the first update of widget B has a deviation of 10 minutes to the expected time: The timer starts at 03:20 when widget A is added, triggers an update for widget A at 03:50, and triggers another update for widget B at 04:20 (instead of 04:10 as expected). 118e41f4b71Sopenharmony_ci> 119e41f4b71Sopenharmony_ci> - Updates at the specified interval and updates at the scheduled time are triggered only when the screen is on. The update action is merely recorded when the screen is off and is performed once the screen is on. 120e41f4b71Sopenharmony_ci> - If the update-through-proxy feature is enabled, periodic update at the scheduled time (either every day or for the next time) does not take effect. 121