Function leptos_dom::helpers::debounce

source ·
pub fn debounce<T: 'static>(
    cx: Scope,
    delay: Duration,
    cb: impl FnMut(T) + 'static
) -> impl FnMut(T)
Expand description

“Debounce” a callback function. This will cause it to wait for a period of delay after it is called. If it is called again during that period, it will wait delay before running, and so on. This can be used, for example, to wrap event listeners to prevent them from firing constantly as you type.

use leptos::{leptos_dom::helpers::debounce, *};

#[component]
fn DebouncedButton(cx: Scope) -> impl IntoView {
    let delay = std::time::Duration::from_millis(250);
    let on_click = debounce(cx, delay, move |_| {
        log!("...so many clicks!");
    });

    view! { cx,
      <button on:click=on_click>"Click me"</button>
    }
}