0Pricing
JavaScript Academy · Lesson

Infinite Scrolling

Load more content as the user scrolls.

Infinite Scrolling is a free JavaScript Academy lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the JavaScript Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

What Is Infinite Scrolling?

Infinite scrolling loads more content automatically as the user nears the bottom of a list, instead of requiring a Next Page click. Social feeds and search results commonly use it.

The Sentinel Pattern

The cleanest technique uses a sentinel: an empty element placed at the end of the list. When the sentinel scrolls into view, you fetch and append the next batch.

// Markup pattern:
// <ul id="feed"> ...items... </ul>
// <div id="sentinel"></div>  <!-- trigger element -->

Observing the Sentinel

Point an Intersection Observer at the sentinel. Because it sits after the last item, its visibility means the user has reached the end.

const sentinel = document.getElementById('sentinel');
const observer = new IntersectionObserver(onIntersect, {
  rootMargin: '200px' // load a bit early
});
observer.observe(sentinel);

Triggering a Load

In the callback, check isIntersecting. When true, load the next page of data.

function onIntersect(entries) {
  entries.forEach(entry => {
    if (entry.isIntersecting) {
      loadMoreItems();
    }
  });
}

Tracking the Page

Keep a page counter so each load fetches the next slice. Increment it after a successful fetch.

let page = 1;

async function loadMoreItems() {
  const items = await fetchPage(page);
  appendItems(items);
  page++;
}

Guarding Against Double Loads

The callback can fire repeatedly. Use a loading flag so a new fetch does not start while one is already in progress.

let loading = false;

async function loadMoreItems() {
  if (loading) return;
  loading = true;
  const items = await fetchPage(page);
  appendItems(items);
  page++;
  loading = false;
}

Appending New Items

Add the fetched items before the sentinel so it stays at the bottom of the growing list, ready to trigger the next load.

function appendItems(items) {
  const feed = document.getElementById('feed');
  items.forEach(item => {
    const li = document.createElement('li');
    li.textContent = item.title;
    feed.appendChild(li);
  });
}

Stopping at the End

When the server returns no more data, disconnect the observer so it stops trying to load. This prevents endless empty requests.

const items = await fetchPage(page);
if (items.length === 0) {
  observer.disconnect(); // no more data
  return;
}

Why rootMargin Helps

A generous rootMargin (like 200px) starts loading before the user actually hits the bottom, so new items appear seamlessly without a visible pause.

new IntersectionObserver(onIntersect, {
  rootMargin: '300px' // begin loading well before the end
});

Accessibility Notes

Infinite scroll can trap keyboard and screen-reader users and hide the footer. Consider a manual Load More button as a fallback and announce new content politely with ARIA live regions.

The Complete Flow

Infinite scroll with Intersection Observer: place a sentinel, observe it, load and append data when it appears, guard against duplicate loads, and disconnect when the data runs out. No scroll math required.

Quick Check

Test your understanding of infinite scrolling.

Recap

You built infinite scrolling:

  • Place a sentinel element after the list.
  • Observe it with an Intersection Observer.
  • On intersection, fetch and append the next page.
  • Use a loading flag to avoid duplicate fetches.
  • Disconnect when data is exhausted.

Next, scroll-triggered animations.

Frequently asked questions

Is the “Infinite Scrolling” lesson free?

Yes — the full text of “Infinite Scrolling” is free to read here on the web, and the JavaScript Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the JavaScript Academy course, upgrade to CoddyKit PRO.

What will I learn in “Infinite Scrolling”?

Load more content as the user scrolls. You practise JavaScript Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start JavaScript Academy?

No prior experience is required. JavaScript Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Infinite Scrolling” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this JavaScript Academy lesson?

Yes. Every JavaScript Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Observing Element Visibility
  2. Lazy Loading Images
  3. Infinite Scrolling
  4. Scroll-Triggered Animations
← Back to JavaScript Academy