Skip to content

Enable debounce / throttling for events #10522

Open

Description

@mkArtakMSFT

This will allow the user to register for scroll events with some time period (100ms) which will filter out events happening within the period, and result in much fewer events being sent to the server.

Activity

  1. added
    enhancement
    area-blazor
    on May 24, 2019
  2. added this to the 3.0.0-preview8 milestone on May 24, 2019
  3. arivoir commented on May 24, 2019

    @arivoir

    Apart from having a delay, the events should be automatically dispatched, and filtered, depending on the real time the event is taking to be processed in the server(notice the network latency and the time involved in process the request counts).

    Something like this

    @functions
    {
        object currentEventValue;
        System.Threading.SemaphoreSlim semaphore;
    
        private async void OnNativeEvent(string value)
        {
            try
            {
                currentEventValue = value;
                await Task.Delay(100);
                await semaphore.WaitAsync();
                if (currentEventValue == value)
                {
                    await CallEventAsync();
                }
            }
            finally
            {
                semaphore.Release();
            }
        }
    }
    
  4. Andrzej-W commented on May 27, 2019

    @Andrzej-W

    I asked for this about year ago. Unfortunately the issue was closed with suggestion that I can do it using some JavaScript code. If it will be implemented it should be possible to use throttling and debouncing in all events. Think about resize events, dragging, or input events when you want to implement incremental search and you don't want to query database after each keystroke.

  5. added
    Needs: Design
    on Jun 24, 2019
  6. modified the milestones: 3.1.0, 5.0.0 on Aug 12, 2019
  7. ctrl-alt-d commented on Aug 18, 2019

    @ctrl-alt-d

    Maybe you can use a Timer as workaround.

  8. removed this from the 5.0.0-preview1 milestone on Jan 10, 2020
  9. 55 remaining items

  10. added this to the .NET 10 Planning milestone on Jan 30, 2024
  11. ericgrantholland commented on Mar 21, 2024

    @ericgrantholland

    I think this is a necessary feature to work with many of the events already exposed on Blazor side, especially for ServerSide Blazor. Really don't want OnMouseOver event firing a million times sending Signal R messages at that rate. I know there is a way around this with custom JS but it's a huge hassle. Need to write both JSSetup and JSCleanup for every event you want to handle.

    Would be really cool if you could set the interval directly from razor syntax, similar to how stopPropagation and preventDefault work now. See below:

    <div @onmouseover="@OnMouseOver" @onmouseover:throttle="160" >
    This div mouse over event throttles 160ms
    </div>
    
    <div @onmouseover="@OnMouseOver" @onmouseover:debounce="500" >
    This div mouse over event debounces 500ms
    </div>
    

    Without this level of control, many already exposed events aren't useful in blazor. Fire too often and cause problems.

  12. MrDach commented on Feb 11, 2025

    @MrDach

    I think this is a necessary feature to work with many of the events already exposed on Blazor side, especially for ServerSide Blazor. Really don't want OnMouseOver event firing a million times sending Signal R messages at that rate. I know there is a way around this with custom JS but it's a huge hassle. Need to write both JSSetup and JSCleanup for every event you want to handle.

    Would be really cool if you could set the interval directly from razor syntax, similar to how stopPropagation and preventDefault work now. See below:

    <div @onmouseover="@OnMouseOver" @onmouseover:throttle="160" >
    This div mouse over event throttles 160ms
    </div>
    
    <div @onmouseover="@OnMouseOver" @onmouseover:debounce="500" >
    This div mouse over event debounces 500ms
    </div>
    

    Without this level of control, many already exposed events aren't useful in blazor. Fire too often and cause problems.

    Agreed that having to do some custom setup to handle this is a hassle.
    Something like the suggested solution would be fantastic!

  13. jvs7315 commented on Apr 2, 2026

    @jvs7315

    Glad to see debouncing on the roadmap for .NET 11; we use Blazor at Penn State, and a built-in way to debounce text input in particular would be very helpful.

  14. added
    on-roadmap
    on Apr 29, 2026
  15. oroztocil commented on Nov 4, 2026

    @oroztocil
    Member

    We are looking into adding a built-in support for rate limiting events on the client. I did some research on the related UX patterns, similar features in other frameworks, and possible pitfalls. The following specification draft describes a design with behavioral choices informed by that context. All feedback is welcome.

    Main scenarios

    1. Incremental text input and live search: Delaying oninput (either directly or with @bind:event="oninput") until the user pauses typing for a short interval (such as 300ms) so the app does not query a backend or re-render on every keystroke.
    2. High-frequency continuous interactions: Rate-limiting onscroll, onmousemove, onpointermove, ontouchmove, ondrag, onwheel, or range slider oninput events so the component receives periodic progress updates without flooding the SignalR circuit or WebAssembly runtime, while still receiving the final position or value when movement stops.
    3. Action rate-limiting and double-click prevention: Preventing rapid repeated triggers on discrete actions such as button onclick or held-down onkeydown events.

    Syntax

    Debounce and throttle are configured as directive attribute parameters on DOM events using an explicit millisecond interval (int):

    <input @oninput="OnSearch" @oninput:debounce="300" />
    
    <input @bind="SearchText" @bind:event="oninput" @oninput:debounce="300" />
    
    <div @onscroll="OnScroll" @onscroll:throttle="100">...</div>
    
    <button @onclick="Save" @onclick:throttle="500">Save</button>
    • The value is an integer representing milliseconds. A value of 0 or less disables rate-limiting, allowing dynamic expressions (for example, @oninput:debounce="@debounceMs").
    • Both :debounce and :throttle are opt-in per event and disabled by default to preserve backwards compatibility.
    • Specifying both :debounce and :throttle on the same event of the same element is a compile-time error.

    Event classification: Discrete vs. Continuous

    When it comes to expectations for when exactly should debounced or throttled DOM events fire to capture the user's intent, there are two types of events:

    • Continuous events represent ongoing state or position changes where the final value matters:
      • Input and selection: oninput, onselect, onselectionchange
      • Pointer, mouse, and touch movement: onmousemove, onpointermove, ontouchmove, onwheel, onmousewheel
      • Dragging and scrolling: ondrag, ondragover, onscroll
      • Media and progress: ontimeupdate, onprogress, onseeking, onvolumechange, onratechange
    • Discrete events represent distinct point-in-time actions or transitions:
      • All other built-in events, including onclick, ondblclick, onmousedown, onmouseup, onkeydown, onkeyup, onkeypress, onchange, onsubmit, onfocus, onblur, ondragstart, ondragend, ondrop, etc.

    Custom events registered by developers or libraries can explicitly specify whether they use discrete or continuous semantics (defaulting to discrete if unspecified).

    Timing semantics

    Debounce (@on{EVENT}:debounce="ms")

    • Both discrete and continuous events use trailing-edge debounce.
    • When the event fires, a timer for ms milliseconds starts. If the same event fires again on the same element before the timer expires, the previous pending event is replaced with the new one and the timer resets.
    • The event is dispatched to .NET only after ms milliseconds have elapsed without another occurrence.

    Throttle (@on{EVENT}:throttle="ms")

    • Discrete events use leading-only throttle.
      • The first event is dispatched immediately and starts an ms cooldown window.
      • Any further occurrences of that event on the element during the cooldown window are ignored. No trailing event fires when the window ends.
    • Continuous events use leading + trailing throttle.
      • The first event is dispatched immediately and starts an ms cooldown window.
      • If one or more occurrences happen during the cooldown window, only the latest one is retained and dispatched when the cooldown window ends, which starts a new cooldown window.
      • This guarantees that the component always receives the final scroll offset, cursor position, or slider value when the user stops moving.

    Behavioral guarantees and edge cases

    1. Flushing pending events to preserve ordering:
      • If an element has a pending debounced or trailing-throttled event (such as oninput), and the element loses focus (blur), commits its value (change), or any non-delayed event in the document (such as submit, click, or an un-delayed keydown) is about to be dispatched to .NET, all pending delayed events are flushed and dispatched immediately first, and their timers are cancelled.
      • For throttled onkeydown / onkeypress, releasing the key (keyup) resets the throttle cooldown so a subsequent intentional key press is not dropped.
    2. Synchronous preventDefault and stopPropagation:
      • When :preventDefault or :stopPropagation is used alongside :debounce or :throttle, the browser's default action or propagation is stopped synchronously on every raw DOM event as it happens, even when the .NET callback dispatch is delayed or dropped.
    3. Capturing event state at occurrence time:
      • Event arguments and input values are captured synchronously when the browser raises the DOM event, not when the timer later expires.
    4. Protecting live user input during re-renders:
      • While an element has an active debounced or throttled input update waiting to be processed, intermediate component re-renders must not overwrite the user's in-progress DOM input value or reset the caret position with stale state.
    5. Component re-renders and disposal:
      • If a component re-renders and replaces the delegate for an event while a timer is pending, the pending timer dispatches to the updated handler when it fires.
      • If the event handler or DOM element is removed, or the component is disposed, any pending timer for it is cancelled immediately and never dispatches to .NET.

    Prototype

    Prototype PRs implementing this spec:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Needs: DesignPillar: Technical DebtPriority:1affected-mediumarea-blazorenhancementfeature-blazor-component-modelon-roadmapseverity-major

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions