xref: /libuv/docs/src/timer.rst (revision 467859c2)
1
2.. _timer:
3
4:c:type:`uv_timer_t` --- Timer handle
5=====================================
6
7Timer handles are used to schedule callbacks to be called in the future.
8
9Timers are either single-shot or repeating. Repeating timers do not adjust
10for overhead but are rearmed relative to the event loop's idea of "now".
11
12Libuv updates its idea of "now" right before executing timer callbacks, and
13right after waking up from waiting for I/O. See also :c:func:`uv_update_time`.
14
15Example: a repeating timer with a 50 ms interval whose callback takes 17 ms
16to complete, runs again 33 ms later. If other tasks take longer than 33 ms,
17the timer callback runs as soon as possible.
18
19Data types
20----------
21
22.. c:type:: uv_timer_t
23
24    Timer handle type.
25
26.. c:type:: void (*uv_timer_cb)(uv_timer_t* handle)
27
28    Type definition for callback passed to :c:func:`uv_timer_start`.
29
30
31Public members
32^^^^^^^^^^^^^^
33
34N/A
35
36.. seealso:: The :c:type:`uv_handle_t` members also apply.
37
38
39API
40---
41
42.. c:function:: int uv_timer_init(uv_loop_t* loop, uv_timer_t* handle)
43
44    Initialize the handle.
45
46.. c:function:: int uv_timer_start(uv_timer_t* handle, uv_timer_cb cb, uint64_t timeout, uint64_t repeat)
47
48    Start the timer. `timeout` and `repeat` are in milliseconds.
49
50    If `timeout` is zero, the callback fires on the next event loop iteration.
51    If `repeat` is non-zero, the callback fires first after `timeout`
52    milliseconds and then repeatedly after `repeat` milliseconds.
53
54    .. note::
55        Does not update the event loop's concept of "now". See :c:func:`uv_update_time` for more information.
56
57        If the timer is already active, it is simply updated.
58
59.. c:function:: int uv_timer_stop(uv_timer_t* handle)
60
61    Stop the timer, the callback will not be called anymore.
62
63.. c:function:: int uv_timer_again(uv_timer_t* handle)
64
65    Stop the timer, and if it is repeating restart it using the repeat value
66    as the timeout. If the timer has never been started before it returns
67    UV_EINVAL.
68
69.. c:function:: void uv_timer_set_repeat(uv_timer_t* handle, uint64_t repeat)
70
71    Set the repeat interval value in milliseconds. The timer will be scheduled
72    to run on the given interval, regardless of the callback execution
73    duration, and will follow normal timer semantics in the case of a
74    time-slice overrun.
75
76    .. note::
77        If the repeat value is set from a timer callback it does not immediately take effect.
78        If the timer was non-repeating before, it will have been stopped. If it was repeating,
79        then the old repeat value will have been used to schedule the next timeout.
80
81.. c:function:: uint64_t uv_timer_get_repeat(const uv_timer_t* handle)
82
83    Get the timer repeat value.
84
85.. c:function:: uint64_t uv_timer_get_due_in(const uv_timer_t* handle)
86
87    Get the timer due value or 0 if it has expired. The time is relative to
88    :c:func:`uv_now()`.
89
90    .. versionadded:: 1.40.0
91
92.. seealso:: The :c:type:`uv_handle_t` API functions also apply.
93