Home Explore Blog CI



neovim

11th chunk of `runtime/doc/luvref.txt`
3edffcc36d3dfde348e9e9b5bf5259b479f6c24751ed2a3e0000000100000fa2
 integer for the
                handle's type (`uv_handle_type`).

                Returns: `string, integer`

==============================================================================
REFERENCE COUNTING                                      *luv-reference-counting*

The libuv event loop (if run in the default mode) will run until there are no
active and referenced handles left. The user can force the loop to exit early
by unreferencing handles which are active, for example by calling |uv.unref()|
after calling |uv.timer_start()|.

A handle can be referenced or unreferenced, the refcounting scheme doesn't use
a counter, so both operations are idempotent.

All handles are referenced when active by default, see |uv.is_active()| for a
more detailed explanation on what being active involves.

==============================================================================
`uv_timer_t` — Timer handle                          *luv-timer-handle* *uv_timer_t*

> |uv_handle_t| functions also apply.

Timer handles are used to schedule callbacks to be called in the future.

uv.new_timer()                                                  *uv.new_timer()*

                Creates and initializes a new |uv_timer_t|. Returns the Lua
                userdata wrapping it.

                Returns: `uv_timer_t userdata` or `fail`

                    >lua
                    -- Creating a simple setTimeout wrapper
                    local function setTimeout(timeout, callback)
                      local timer = uv.new_timer()
                      timer:start(timeout, 0, function ()
                        timer:stop()
                        timer:close()
                        callback()
                      end)
                      return timer
                    end

                    -- Creating a simple setInterval wrapper
                    local function setInterval(interval, callback)
                      local timer = uv.new_timer()
                      timer:start(interval, interval, function ()
                        callback()
                      end)
                      return timer
                    end

                    -- And clearInterval
                    local function clearInterval(timer)
                      timer:stop()
                      timer:close()
                    end
<

uv.timer_start({timer}, {timeout}, {repeat}, {callback})      *uv.timer_start()*

                > method form `timer:start(timeout, repeat, callback)`

                Parameters:
                - `timer`: `uv_timer_t userdata`
                - `timeout`: `integer`
                - `repeat`: `integer`
                - `callback`: `callable`

                Start the timer. `timeout` and `repeat` are in milliseconds.

                If `timeout` is zero, the callback fires on the next event
                loop iteration. If `repeat` is non-zero, the callback fires
                first after `timeout` milliseconds and then repeatedly after
                `repeat` milliseconds.

                Returns: `0` or `fail`

uv.timer_stop({timer})                                         *uv.timer_stop()*

                > method form `timer:stop()`

                Parameters:
                - `timer`: `uv_timer_t userdata`

                Stop the timer, the callback will not be called anymore.

                Returns: `0` or `fail`

uv.timer_again({timer})                                       *uv.timer_again()*

                > method form `timer:again()`

                Parameters:
                - `timer`: `uv_timer_t userdata`

                Stop the timer, and if it is repeating restart it using the
                repeat value as the timeout. If the timer has never been
                started before it raises `EINVAL`.

                Returns: `0` or `fail`

uv.timer_set_repeat({timer}, {repeat})                   *uv.timer_set_repeat()*

                > method form `timer:set_repeat(repeat)`


Title: Libuv: Reference Counting, Timer Handles, and Timer Functions
Summary
This section covers libuv's reference counting mechanism, which determines when the event loop exits based on active and referenced handles. It introduces the `uv_timer_t` handle and describes functions for creating, starting, stopping, and controlling timer behavior: `uv.new_timer()`, `uv.timer_start()`, `uv.timer_stop()`, `uv.timer_again()`. Example Lua code demonstrates how to create `setTimeout` and `setInterval` wrappers using these functions.