定时任务
{ "name": "blueos.app.appmanager.schedule" }import schedule from '@blueos.app.appmanager.schedule' 或 const schedule = require('@blueos.app.appmanager.schedule')schedule.scheduleJob(OBJECT)
Section titled “schedule.scheduleJob(OBJECT)”设置定时任务
| 属性 | 类型 | 必填 | 说明 |
|---|---|---|---|
| type | Number | 是 | 1 硬件时间,2 真实时间流逝,前者可以通过修改系统时间触发triggerMethod,后者要通过真实时间的流逝,即使在休眠状态,时间也会被计算 |
| timeout | long | 是 | 若 type 为 1,则为首次执行时间的时间戳,即从 1970/01/01 00:00:00 GMT 到当前时间的毫秒数;若 type 为 2,则为当前时间距离首次执行时间的间隔,单位毫秒。 |
| triggerMethod | String | 是 | app.ux 中定义的方法名,由后台拉起时调用 |
| interval | long | 否 | 周期性执行的间隔,毫秒为单位,不传就不重复执行 |
| params | Object | 否 | 任务参数 |
| success | Function | 否 | 成功回调 |
| fail | Function | 否 | 失败回调 |
| complete | Function | 否 | 执行结束后的回调 |
- 首次执行时间可设置为过去的时间
- 若首次执行时间为过去时间,已过期的任务将不会被执行,未过期的任务仍然会被执行
| 返回值 | 类型 | 说明 |
|---|---|---|
| id | Integer | 底层分配唯一的 ID |
fail 返回错误代码
Section titled “fail 返回错误代码”| 错误码 | 说明 |
|---|---|
| -27 | 定时任务已满 |
| -28 | 已注册 |
// xx.uxschedule.scheduleJob({ type: 1, timeout: new Date('2050-10-01 09:00:00').getTime(), interval: 1000, triggerMethod: 'scheduleFunc', params: { color: 'red', }, success: function (data) { console.log(`handling success, scheduleId = ${data.id}`) }, fail: function (data, code) { console.log(`handling fail,code = ${code}`) }, complete: function () { console.log(`handling complete`) },})
// app.uxexport default { scheduleFunc(params) { console.log(`background processing color = ${params.color}`) },}schedule.cancel(id: Integer)
Section titled “schedule.cancel(id: Integer)”取消定时任务
| 返回值 | 类型 | 说明 |
|---|---|---|
| id | Integer | 底层分配唯一的 ID |
| 返回值 | 类型 | 说明 |
|---|---|---|
| value | Boolean | true:成功; false:失败; |
schedule.cancel(1)