Scrollend Event Replaces Scroll Debounce

Listen for scrollend instead of debounced scroll events: it fires once scrolling has fully settled, which is the right moment to append a page.

TypescriptData Fetching

Code

typescriptsnippet
25 lines
// 'scroll' fires continuously while scrolling; the 'scrollend' event fires
// once, after scrolling has fully settled - so it replaces hand-rolled
// debounce timers when you only care about the resting position
let settled = 0;
let scrollEvents = 0;

function attachFeedMonitoring(scroller: HTMLElement): void {
  scroller.addEventListener("scroll", () => {
    scrollEvents += 1;
  });
  scroller.addEventListener("scrollend", () => {
    settled += 1;
    console.log(`scrollend #${settled} after ${scrollEvents} scroll events; append next page now`);
  });
}

if (typeof document === "undefined") {
  console.log("scrollend needs a browser DOM with a scrollable #log-feed element");
} else {
  const scroller = document.querySelector<HTMLElement>("#log-feed");
  if (scroller) {
    attachFeedMonitoring(scroller);
  }
  console.log("listener attached:", Boolean(scroller));
}

Output

Captured from a sandboxed run of this snippet in the build pipeline.

scrollend needs a browser DOM with a scrollable #log-feed element

The shape of the program

The engaged pieces are the function attachFeedMonitoring.

Watch out for

  • Runs with what the toolchain already ships — nothing extra.
  • The snippet leans on document — browser-only surfaces a bare Node process cannot provide.

Applying it

Entry signature: function attachFeedMonitoring(scroller: HTMLElement)

The demo output is labeled (listener attached:), so every printed line carries the fingerprint of its operation.

Statements that matter

scroller.addEventListener("scrollend", () => {
  settled += 1;
  console.log(`scrollend #${settled} after ${scrollEvents} scroll events; append next page now`);
});
scroller.addEventListener("scroll", () => {
  scrollEvents += 1;
});

Interactive Tools for This Topic

FAQ

Does Scrollend Event Replaces Scroll Debounce run in plain Node.js?
No — it touches `document`, which exist only in the browser. Run it in a page (or the DevTools console) instead of a bare Node process.