Under The Hood
frontend htmx HTMX 1.x / 2.x

hx-trigger: Event Filters, Debounce (delay), and Throttle Modifiers

Last updated
Prerequisites:
DOM Events
Debounce and Throttle algorithms
  • htmx
  • hx-trigger
  • debounce
  • throttle
  • events
  • filter

Read at your depth

The practical view

`hx-trigger` customizes which events fire an HTMX request and applies rate-limiting modifiers. Common syntax: `hx-trigger="keyup changed delay:500ms"` (debounced search), `hx-trigger="click throttle:1s"` (prevents double-clicks), `hx-trigger="load"` (fires on page load), `hx-trigger="revealed"` (fires when element scrolls into view). You can also use event filters: `hx-trigger="click[ctrlKey]"`.

The same idea in other frameworks

react equivalent
Custom `useDebounce(val, 500)` hook inside `useEffect`.

Legacy vs modern

RxJS/Lodash Debounce Implementation vs hx-trigger Modifiers

Debouncing live search manually requires external libraries and state; `hx-trigger` configures debounce in one line.

before → after
Manual JS Debounce Search
let timer; input.onkeyup = () => { clearTimeout(timer); timer = setTimeout(fetchResults, 500); }; // ❌ Verbose
Declarative HTMX Debounced Search
<input hx-get="/search" hx-trigger="keyup changed delay:500ms" hx-target="#results" /> // ✅ NEW

Interview gotchas

Context

Tests your understanding of debounce vs throttle algorithms in UI events.

The mechanical answer

`delay:500ms` is a **debounce**: it waits until 500ms have elapsed *since the last event* before firing the request (ideal for typing in search boxes). `throttle:500ms` is a **rate limiter**: it fires the request immediately on first trigger, and then ignores all subsequent triggers for the next 500ms (ideal for preventing double-clicking on submit buttons).

Trap

Using `throttle` instead of `delay` on a search input will cause intermediate requests to fire mid-word while typing.

Under The Hood — a multi-depth technical interview hub.

Third-party licenses Press ⌘ K to search.