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