跳转到内容

定时任务

{ "name": "blueos.app.appmanager.schedule" }
import schedule from '@blueos.app.appmanager.schedule' const schedule = require('@blueos.app.appmanager.schedule')

设置定时任务

属性类型必填说明
typeNumber1 硬件时间,2 真实时间流逝,前者可以通过修改系统时间触发triggerMethod,后者要通过真实时间的流逝,即使在休眠状态,时间也会被计算
timeoutlongtype1,则为首次执行时间的时间戳,即从 1970/01/01 00:00:00 GMT 到当前时间的毫秒数;若 type2,则为当前时间距离首次执行时间的间隔,单位毫秒。
triggerMethodStringapp.ux 中定义的方法名,由后台拉起时调用
intervallong周期性执行的间隔,毫秒为单位,不传就不重复执行
paramsObject任务参数
successFunction成功回调
failFunction失败回调
completeFunction执行结束后的回调
  1. 首次执行时间可设置为过去的时间
  2. 若首次执行时间为过去时间,已过期的任务将不会被执行,未过期的任务仍然会被执行
返回值类型说明
idInteger底层分配唯一的 ID
错误码说明
-27定时任务已满
-28已注册
// xx.ux
schedule.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.ux
export default {
scheduleFunc(params) {
console.log(`background processing color = ${params.color}`)
},
}

取消定时任务

返回值类型说明
idInteger底层分配唯一的 ID
返回值类型说明
valueBooleantrue:成功; false:失败;
schedule.cancel(1)