首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >有办法每天早上8:00在vibed上运行任务吗?

有办法每天早上8:00在vibed上运行任务吗?
EN

Stack Overflow用户
提问于 2017-04-11 13:16:26
回答 1查看 94关注 0票数 1

我每天早上8:00在一个vibe.d网络应用程序中运行一个任务。目前,我使用带有周期参数的setTimer函数作为true。但是这样的话,我无法精确地控制触发任务的时间。有什么简单的方法吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-04-11 18:10:33

谢天谢地,我就是这么做的。我计算下一个上午8点之前的时间,然后打电话给setTimer。下面是供进一步参考的代码:

代码语言:javascript
复制
void startDailyTaskAtTime(TimeOfDay time, void delegate() task) {
  // Get the current date and time
  DateTime now = cast(DateTime)Clock.currTime();

  // Get the next time occurrence
  DateTime nextOcc = cast(DateTime)now;
  if (now.timeOfDay >= time) {
    nextOcc += dur!"days"(1);
  }
  nextOcc.timeOfDay = time;

  // Get the duration before now and the next occurrence
  Duration timeBeforeNextOcc = nextOcc - now;

  void setDailyTask() {
    // Run the task once
    task(); 
    // Run the task all subsequent days at the same time
    setTimer(1.days, task, true);
  }

  setTimer(timeBeforeNextOcc, &setDailyTask);
}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43347079

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档