1e41f4b71Sopenharmony_ci# Transient Task (ArkTS)
2e41f4b71Sopenharmony_ci
3e41f4b71Sopenharmony_ci
4e41f4b71Sopenharmony_ci## Overview
5e41f4b71Sopenharmony_ci
6e41f4b71Sopenharmony_ciAn application is suspended after it runs in the background for a short period of time. If the application needs to execute a short-time task in the background, for example, saving the status, it can request a transient task to extend the running time in the background.
7e41f4b71Sopenharmony_ci
8e41f4b71Sopenharmony_ci
9e41f4b71Sopenharmony_ci## Constraints
10e41f4b71Sopenharmony_ci
11e41f4b71Sopenharmony_ci- **When to request**: An application can request a transient task when it is running in the foreground or through the **onBackground** callback.
12e41f4b71Sopenharmony_ci
13e41f4b71Sopenharmony_ci- **Quantity limit**: An application can request a maximum of three transient tasks during a time segment. As shown in the figure below, the application requests two transient tasks in the time segments ①, ②, and ③, and one transient task in the time segment ④.
14e41f4b71Sopenharmony_ci
15e41f4b71Sopenharmony_ci- **Quota mechanism**: An application has a certain quota for transient tasks (adjusted based on the system status and user habits). The default quota for a single day (within 24 hours) is 10 minutes, and the maximum quota for each request is 3 minutes. In case of [low battery](../reference/apis-basic-services-kit/js-apis-battery-info.md), the default quota for each request is 1 minute. After the quota is used up, the application cannot request transient tasks anymore. The system also provides an API for an application to query the remaining duration of a transient task so as to determine whether to continue running other services.
16e41f4b71Sopenharmony_ci
17e41f4b71Sopenharmony_ci- **Quota calculation**: Transient tasks are timed only when the application is running in the background. If the application has multiple transient tasks during the same time segment, no repeated timing is performed. As in the figure below, the application has two transient tasks, A and B. Task A is requested when the application is running in the foreground, and the timing starts when the application switches to the background (marked as ①). When the application switches to the foreground, the timing stops (marked as ②). When the application switches to the background again, the timing starts again (marked as ③). When task A is finished, task B still exists, and therefore the timing continues (marked as ④). In this process, the total time consumed by the transient tasks is ①+③+④. 
18e41f4b71Sopenharmony_ci  
19e41f4b71Sopenharmony_ci  **Figure 1** Quota calculation for transient tasks
20e41f4b71Sopenharmony_ci  
21e41f4b71Sopenharmony_ci  ![transient-task](figures/transient-task.png)
22e41f4b71Sopenharmony_ci
23e41f4b71Sopenharmony_ci  > **NOTE**
24e41f4b71Sopenharmony_ci  >
25e41f4b71Sopenharmony_ci  > The application shall proactively cancel a transient task when it is finished. Otherwise, the time frame allowed for the application to run in the background will be affected.
26e41f4b71Sopenharmony_ci
27e41f4b71Sopenharmony_ci- **Timeout**: If a transient task is about to time out, the system notifies the application of the timeout by using a callback. The application needs to cancel the task. Otherwise, the system forcibly terminates the application process.
28e41f4b71Sopenharmony_ci
29e41f4b71Sopenharmony_ci## Available APIs
30e41f4b71Sopenharmony_ci
31e41f4b71Sopenharmony_ciThe table below lists the main APIs used for transient task development. For details about more APIs and their usage, see [Background Task Management](../reference/apis-backgroundtasks-kit/js-apis-resourceschedule-backgroundTaskManager.md).
32e41f4b71Sopenharmony_ci
33e41f4b71Sopenharmony_ci**Table 1** Main APIs for transient tasks
34e41f4b71Sopenharmony_ci
35e41f4b71Sopenharmony_ci| API| Description|
36e41f4b71Sopenharmony_ci| -------- | -------- |
37e41f4b71Sopenharmony_ci| requestSuspendDelay(reason: string, callback: Callback<void>): DelaySuspendInfo | Requests a transient task.|
38e41f4b71Sopenharmony_ci| getRemainingDelayTime(requestId: number): Promise<number> | Obtains the remaining time of a transient task.|
39e41f4b71Sopenharmony_ci| cancelSuspendDelay(requestId: number): void | Cancels a transient task.|
40e41f4b71Sopenharmony_ci
41e41f4b71Sopenharmony_ci
42e41f4b71Sopenharmony_ci## How to Develop
43e41f4b71Sopenharmony_ci
44e41f4b71Sopenharmony_ci1. Import the module.
45e41f4b71Sopenharmony_ci   
46e41f4b71Sopenharmony_ci   ```ts
47e41f4b71Sopenharmony_ci   import { backgroundTaskManager } from '@kit.BackgroundTasksKit';
48e41f4b71Sopenharmony_ci   import { BusinessError } from '@kit.BasicServicesKit';
49e41f4b71Sopenharmony_ci   ```
50e41f4b71Sopenharmony_ci
51e41f4b71Sopenharmony_ci2. Request a transient task and implement the callback.
52e41f4b71Sopenharmony_ci   
53e41f4b71Sopenharmony_ci   ```ts
54e41f4b71Sopenharmony_ci   let id: number;         // ID of the transient task.
55e41f4b71Sopenharmony_ci   let delayTime: number;  // Remaining time of the transient task.
56e41f4b71Sopenharmony_ci
57e41f4b71Sopenharmony_ci   // Request a transient task.
58e41f4b71Sopenharmony_ci   function requestSuspendDelay() {
59e41f4b71Sopenharmony_ci     let myReason = 'test requestSuspendDelay'; // Reason for the request.
60e41f4b71Sopenharmony_ci     let delayInfo = backgroundTaskManager.requestSuspendDelay(myReason, () => {
61e41f4b71Sopenharmony_ci       // Callback function, which is triggered when the transient task is about to time out. The application can carry out data clear and annotation, and cancel the task in the callback.
62e41f4b71Sopenharmony_ci       console.info('suspend delay task will timeout');
63e41f4b71Sopenharmony_ci       backgroundTaskManager.cancelSuspendDelay(id);
64e41f4b71Sopenharmony_ci     })
65e41f4b71Sopenharmony_ci     id = delayInfo.requestId;
66e41f4b71Sopenharmony_ci     delayTime = delayInfo.actualDelayTime;
67e41f4b71Sopenharmony_ci   }
68e41f4b71Sopenharmony_ci   ```
69e41f4b71Sopenharmony_ci
70e41f4b71Sopenharmony_ci3. Obtain the remaining time of the transient task. Based on the remaining time, the application determines whether to continue to run other services. For example, the application has two small tasks. After the first task is executed, it queries the remaining time of the current transient task to determine whether to execute the second task.
71e41f4b71Sopenharmony_ci   
72e41f4b71Sopenharmony_ci   ```ts
73e41f4b71Sopenharmony_ci   let id: number; // ID of the transient task.
74e41f4b71Sopenharmony_ci
75e41f4b71Sopenharmony_ci   async function getRemainingDelayTime() {
76e41f4b71Sopenharmony_ci     backgroundTaskManager.getRemainingDelayTime(id).then((res: number) => {
77e41f4b71Sopenharmony_ci       console.info('Succeeded in getting remaining delay time.');
78e41f4b71Sopenharmony_ci     }).catch((err: BusinessError) => {
79e41f4b71Sopenharmony_ci       console.error(`Failed to get remaining delay time. Code: ${err.code}, message: ${err.message}`);
80e41f4b71Sopenharmony_ci     })
81e41f4b71Sopenharmony_ci   }
82e41f4b71Sopenharmony_ci   ```
83e41f4b71Sopenharmony_ci
84e41f4b71Sopenharmony_ci4. Cancel the transient task.
85e41f4b71Sopenharmony_ci   
86e41f4b71Sopenharmony_ci   ```ts
87e41f4b71Sopenharmony_ci   let id: number; // ID of the transient task.
88e41f4b71Sopenharmony_ci  
89e41f4b71Sopenharmony_ci   function cancelSuspendDelay() {
90e41f4b71Sopenharmony_ci     backgroundTaskManager.cancelSuspendDelay(id);
91e41f4b71Sopenharmony_ci   }
92e41f4b71Sopenharmony_ci   ```
93e41f4b71Sopenharmony_ci
94