EvTimer
final class EvTimer extends EvWatcher (View source)
Class EvTimer
EvTimer watchers are simple relative timers that generate an event after a given time, and optionally repeating in regular intervals after that.
The timers are based on real time, that is, if one registers an event that times out after an hour and resets the system clock to January last year, it will still time out after( roughly) one hour. "Roughly" because detecting time jumps is hard, and some inaccuracies are unavoidable.
The callback is guaranteed to be invoked only after its timeout has passed (not at, so on systems with very low-resolution clocks this might introduce a small delay). If multiple timers become ready during the same loop iteration then the ones with earlier time-out values are invoked before ones of the same priority with later time-out values (but this is no longer true when a callback calls EvLoop::run() recursively).
The timer itself will do a best-effort at avoiding drift, that is, if a timer is configured to trigger every 10 seconds, then it will normally trigger at exactly 10 second intervals. If, however, the script cannot keep up with the timer (because it takes longer than those 10 seconds to do) the timer will not fire more than once per event loop iteration.
Properties
bool | $is_active | from EvWatcher | |
bool | $is_pending | from EvWatcher | |
mixed | $data | from EvWatcher | |
int | $priority | from EvWatcher | |
float | $repeat | ||
float | $remaining |
Methods
Constructs an EvTimer watcher object.
Invokes the watcher callback with the given received events bit mask.
Restarts the timer watcher.
Configures the watcher.
Creates a stopped EvTimer watcher object.
Details
__construct(float $after, float $repeat, callable $callback, mixed $data = null, int $priority = 0)
Constructs an EvTimer watcher object.
int
clear()
Clear watcher pending status.
If the watcher is pending, this method clears its pending status and returns its revents bitset (as if its callback was invoked). If the watcher isn't pending it does nothing and returns 0.
Sometimes it can be useful to "poll" a watcher instead of waiting for its callback to be invoked, which can be accomplished with this function.
feed(int $revents)
Feeds the given revents set into the event loop.
Feeds the given revents set into the event loop, as if the specified event had happened for the watcher.
EvLoop
getLoop()
Returns the loop responsible for the watcher.
invoke(int $revents)
Invokes the watcher callback with the given received events bit mask.
keepalive(bool $value = true)
Configures whether to keep the loop from returning.
Configures whether to keep the loop from returning. With keepalive value set to FALSE the watcher won't keep Ev::run() / EvLoop::run() from returning even though the watcher is active.
Watchers have keepalive value TRUE by default.
Clearing keepalive status is useful when returning from Ev::run() / EvLoop::run() just because of the watcher is undesirable. It could be a long running UDP socket watcher or so.
setCallback(callable $callback)
Sets new callback for the watcher.
start()
Starts the watcher.
Marks the watcher as active. Note that only active watchers will receive events.
stop()
Stops the watcher.
Marks the watcher as inactive. Note that only active watchers will receive events.
again()
Restarts the timer watcher.
This will act as if the timer timed out and restart it again if it is repeating. The exact semantics are:
- if the timer is pending, its pending status is cleared.
- if the timer is started but non-repeating, stop it (as if it timed out).
- if the timer is repeating, either start it if necessary (with the repeat value), or reset the running timer to the repeat value.
set(float $after, float $repeat)
Configures the watcher.
final static EvTimer
createStopped(float $after, float $repeat, mixed $callback, mixed $data = null, int $priority = 0)
Creates a stopped EvTimer watcher object.