Determines the minimum amount of time that elapses before the timer fires.
A delegate to be called when the timer fires. Can be null, in which case the timer will not do anything.
Speficies if the timer fires repeatedly or only once
Returns a Timer object that can be used to identify and modify the timer.
Periodic timers will trigger for the first time after timeout, then at best every timeout period after this. Periodic timers may be explicitly stopped by calling the Timer.stop() method on the return value of this function.
As timer are non-preemtive (see the "Preemption" section), user code does not need to compensate for time drift, as the time spent in the function will not affect the frequency, unless the function takes longer to complete than the timer.
Preemption: Like other events in Vibe.d, timers are non-preemptive, meaning that the currently executing function will not be interrupted to let a timer run. This is usually not a problem in server applications, as any blocking code will be easily noticed (the server will stop to handle requests), but might come at a surprise in code that doesn't handle request. If this is a problem, the solution is usually to either explicitly give control to the event loop (by calling yield) or ensuring operations are asynchronous (e.g. call functions from vibe.core.file instead of std.file).
Reentrancy: The event loop guarantees that the same timer will never be called more than once at a time. Hence, functions run on a timer do not need to be re-entrant, even if they execute for longer than the timer frequency.
void printTime() @safe nothrow { import std.datetime; logInfo("The time is: %s", Clock.currTime()); } void test() { import vibe.core.core; // start a periodic timer that prints the time every second setTimer(1.seconds, toDelegate(&printTime), true); }
createTimer
Creates a new timer, that will fire callback after timeout
Timers can be be separated into two categories: one-off or periodic. One-off timers fire only once, after a specific amount of time, while periodic timer fire at a regular interval.
One-off_timers: One-off timers can be used for performing a task after a specific delay, or to schedule a time without interrupting the currently running code. For example, the following is a way to emulate a 'schedule' primitive, a way to schedule a task without starting it immediately (unlike runTask):
In this example, the server delays the network communication that will be performed by sendToPeers until after the request is fully processed, ensuring the client doesn't wait more than the actual processing time for the response.