2017-04-11 23 views
1

我试图每天早上8:00在vibe.d web应用程序中运行一项任务。 目前,我使用setTimer函数,定期参数为true。但这样,我无法精确控制任务触发的时间。有没有简单的方法来做到这一点振动?有没有办法在每天上午8:00以震动方式运行任务?

+2

没有什么能够阻止你计算时间直到下一个8:00 AM并且调用具有适当持续时间的'setTimer'。 – sigod

回答

2

谢谢你,这正是我所做的。我计算直到下午8:00的时间并调用setTimer。以下是供进一步参考的代码:

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); 
} 
相关问题