如何实现定时任务有懂的吗?

使用arkts实现比如定时30分钟去请求一个网页,目前是使用定时器 setInterval() 函数,代码入下:

let timeout = 1000*60*30;
  timeout = 1000*60*2;
  let that = this;
  if (this.timeoutID == 0) {
    this.startTime();
    let timeoutID = setInterval(function (){
      console.info('timeoutID')
      that.btnTimeText = '定时器已启动';
      that.timeoutID = timeoutID;
      that.timeoutCount = that.timeoutCount + 1;
      console.info('timeoutCount=' + that.timeoutCount )
      that.btnTimeText = '定时器已启动 ' + that.timeoutCount;
    },timeout);
  } else {
    that.btnTimeText = '启动定时器';
    clearTimeout(that.timeoutID);
    that.timeoutID = 0
  }
})
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.

这个代码只能页面一直显示,页面不能切换,手机不能休眠。如果可以向后台程序一直运行,不管页面是不是在前台,手机是不是锁屏


Ability框架
UI
2024-01-04 15:23:13
浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
Florenc

看你需求,直接使用setTimeout即可,如果需要切后台也可用,那只能用延时任务 ​https://developer.harmonyos.com/cn/docs/documentation/doc-guides-V3/work-scheduler-0000001711831844-V3​

分享
微博
QQ
微信
回复
2024-01-04 16:31:13
相关问题
如何设置组件定时任务定时
1472浏览 • 1回复 待解决
请问用什么接口可以实现定时任务
2543浏览 • 0回复 待解决
如何实现时任务申请和回调?
997浏览 • 1回复 待解决
FluentData MySql
2792浏览 • 1回复 待解决
Livedata相关
1305浏览 • 1回复 待解决
如何编译底层so
2856浏览 • 1回复 待解决
redis是什么
3951浏览 • 1回复 待解决
Kubernetes 创建集群
2935浏览 • 1回复 待解决
proxyro用户问题
5172浏览 • 1回复 待解决
如何申请多个长时任务
2834浏览 • 1回复 待解决
HarmonyOS 延时任务
657浏览 • 1回复 待解决
Unable to install Toolchains
8231浏览 • 1回复 待解决
centos ssh 问题
2650浏览 • 1回复 待解决
vpp 编译问题
2667浏览 • 1回复 待解决
什么是MongoDB?
4288浏览 • 1回复 待解决
SkyWalking访问问题
2982浏览 • 1回复 待解决
诡异MySQL connect
2263浏览 • 1回复 待解决
ZigBee技术是什么
5633浏览 • 1回复 待解决