pub fn use_timeout<'hook, Callback>(
callback: Callback,
millis: u32,
) -> impl 'hook + Hook<Output = UseTimeoutHandle>where
Callback: FnOnce() + 'static + 'hook,
Expand description
A hook that schedules a timeout to invoke callback
in millis
milliseconds from now.
The timeout will be cancelled if millis
is set to 0 or cancel()
is called.
§Example
use yew_hooks::prelude::*;
#[function_component(Timeout)]
fn timeout() -> Html {
let state = use_state(|| 0);
let timeout = {
let state = state.clone();
use_timeout(move || {
state.set(*state + 1);
}, 2000)
};
let onreset = {
let timeout = timeout.clone();
Callback::from(move |_| timeout.reset())
};
let oncancel = {
let timeout = timeout.clone();
Callback::from(move |_| timeout.cancel())
};
html! {
<>
<button onclick={onreset}>{ "Reset timeout" }</button>
<button onclick={oncancel}>{ "Cancel timeout" }</button>
{ *state }
</>
}
}
§Note
When used in function components and hooks, this hook is equivalent to:
pub fn use_timeout<Callback>(callback: Callback, millis: u32) -> UseTimeoutHandle
where
Callback: FnOnce() + 'static,
{
/* implementation omitted */
}