Skip to content

How to use timeout to delay signal changes? #304

Answered by gbj
dgllghr asked this question in Q&A
Discussion options

You must be logged in to vote

I wonder if the problem is actually coming from somewhere else in your code. I added stubs for a few bits that were missing from your sample, and is seems to work as intended. You wrote a pretty elegant debounce, here!

use gloo_timers::callback::Timeout;

#[component]
pub fn Search(cx: Scope) -> impl IntoView {
    let (q, set_q) = create_signal(cx, String::new());
    let (q_delayed, set_q_delayed) = create_signal(cx, q.get_untracked());
    
    // Add a delay so that the search happens when a key has _not_ been pressed for the
    // delay period
    create_effect(cx, move |to: Option<Timeout>| {
        if let Some(t) = to {
            t.cancel();
        }
        let value = q();

Replies: 1 comment 2 replies

Comment options

You must be logged in to vote
2 replies
@dgllghr
Comment options

@gbj
Comment options

gbj Jan 13, 2023
Maintainer

Answer selected by dgllghr
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants