Skip to content

Using debounce for search input - first run #174

Answered by outbackStack
outbackStack asked this question in Q&A
Discussion options

You must be logged in to vote

I've added in a signal to track the first run and it works with this code.
Thank you for making this debounce package. It's awesome :-)

  const [isFirstRun, setIsFirstRun] = createSignal(true);

  createEffect(
    on(value, () => {
      let trigger;
      if (isFirstRun()) {
        console.log("run for the first time...");
        handleSubmit();
        setIsFirstRun(false);
      }

      if (!isFirstRun()) {
        console.log("run for the second time...");
        trigger = debounce(() => handleSubmit(), 500);
        trigger();
      }

      return () => trigger.clear();
    })
  );

Replies: 1 comment 2 replies

Comment options

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

@outbackStack
Comment options

Answer selected by outbackStack
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