Skip to content

Debounced Binding #9173

@webJose

Description

@webJose

Describe the problem

While creating this example REPL about how to create a timer, I came to realize that directly binding an input field to the variable that triggers the start of the timer results in an immediate start of the timer. But what if the user is not done typing? Then the timer continuously restarts with every keystroke.

Not only does this look "bad" in a sense, but it is a performance hit.

So what's the usual option? To debounce the input event, but oh oh! I'm not handling the input event. I am using value binding. I have no place to debounce.

Describe the proposed solution

It would be ideal if Svelte could provide debouncing on binding. Something like this:

<input type="number" bind:value|debounced={variable} /> <!-- default debounce time.  400ms? -->
<input type="number" bind:value|debounced={[variable, debounceTime]} />

In the example, variable is the bound variable, while debouncedTime is a variable containing the number of milliseconds to debounce the binding event.

Alternatives considered

Drop Binding

The obvious one: Renounce the excellent Svelte feature and instead handle the input event in the "classic" way.

Bind to another Variable.

In the example REPL, the bound variable is countFrom. This is passed to the Timer component, which triggers the timer. In order to debounce this, bind the input field to a new variable, and then use reactivity ($:) to start the debounce. On timeout, the variable countFrom would be updated.

Importance

would make my life easier

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions