Debounce Input Requests with htmx’s hx-trigger delay and changed Modifiers
Learn how to use htmx’s hx-trigger with the delay and changed modifiers to debounce user input, cut unnecessary server calls, and keep your UI responsive.
13 Dec 2025, 13:44 UTC

Problem: Too many requests while typing
When a search box fires a request on every keyup, fast typing can generate dozens of calls in a second. Most of those requests are wasted because the user hasn’t finished the term, and if the server is slow the UI can feel laggy or show stale results.
How hx‑trigger with delay and changed works
The hx-trigger attribute lets you combine DOM events with modifiers. Adding changed means the trigger only fires when the element’s value actually differs from the previous value, preventing duplicate submissions for repeated keys. The delay: modifier starts a timer each time the event occurs; if another event arrives before the timer expires, the timer resets. The request is sent only after the value has stayed unchanged for the specified interval, giving a simple debounce.
Worked example: live search
<input type="text" name="q" placeholder="Search…"
hx-get="/search"
hx-trigger="keyup changed delay:300ms"
hx-target="#results"
hx-indicator="#indicator">
<div id="indicator" class="htmx-indicator">Loading…</div>
<div id="results"></div>
As the user types, the network tab shows a request no sooner than 300 ms after the last keystroke, and typing the same character twice produces only one request. The indicator appears while the request is in flight, giving feedback without blocking the UI.
Trade‑offs and limitations
- If the endpoint is slow or returns large payloads, the user may still perceive lag; pairing the trigger with
hx-indicatorand optionally disabling the input (disabledduring the request) can improve perceived performance. - The
delaymodifier works reliably with standard DOM events likekeyup,keydown, orinput. For custom events you must verify that the event bubbles and is cancellable, or consider an extension for more complex debouncing logic. - Because htmx aborts the previous XMLHttpRequest/fetch when a new trigger occurs, only the most recent request reaches the server, which avoids race conditions but also means any in‑flight work is discarded.
Actionable closing
- Add the
hx-triggerline shown above to any input that drives an Ajax call. - Open the browser’s Network tool, type a few words, and confirm requests appear only after the delay and that repeated characters do not duplicate calls.
- Experiment with the delay value (e.g., 100 ms for a more responsive feel or 600 ms to save bandwidth) and observe the spacing change in the Network tab.
- If you notice sluggishness, add
hx-indicatorand consider temporarily disabling the input while the request is pending.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.