Skip to content
  • Home
  • About
  • No Access
  • Privacy Policy
Bits of .NET
Bits of .NET
  • C#
  • Asp.Net Core
  • SQL
  • Windows
  • WordPress
  • AI
  • JavaScript
  • CSS
  • MORE
    • HTML
    • Ajax
    • Git
    • Docker
    • Kubernetes
    • Photoshop
    • Visual Studio

Bits of .NET

Daily micro-tips for C#, SQL, performance, and scalable backend engineering.

  • C#
  • Asp.Net Core
  • SQL
  • Windows
  • WordPress
  • AI
  • JavaScript
  • CSS
  • MORE
    • HTML
    • Ajax
    • Git
    • Docker
    • Kubernetes
    • Photoshop
    • Visual Studio
JavaScript

JavaScript: Fix an IntersectionObserver Firing Only Once

- 29.11.26 - ErcanOPAK

⚡ Fix an IntersectionObserver Firing Only Once

An IntersectionObserver callback runs once immediately when you call observe(), reporting the target’s current intersection state before anything has actually changed, and again every time that state crosses one of your configured thresholds afterward. A callback that calls unobserve() unconditionally on its first invocation — written assuming the first call always means “it just became visible” — ends up unobserving the target during that very first synchronous report if the element happens to start out of view, so the observer never gets a chance to fire again when the element actually does scroll into frame.

🔎 The Problem

const observer = new IntersectionObserver((entries) => {
  entries.forEach((entry) => {
    loadContent(entry.target);
    observer.unobserve(entry.target); // unobserves on EVERY callback,
                                        // including the initial report
                                        // where isIntersecting is false
  });
});
observer.observe(el); // el starts off-screen -> callback fires once
                       // with isIntersecting:false -> unobserved
                       // before it ever scrolls into view

✅ Fix: An IntersectionObserver Firing Only Once

  • Check entry.isIntersecting before acting on a callback, and only call unobserve() when it’s true, so the observer stays attached through the initial “not yet visible” report.
  • Log the entries array during development for any lazy-load or reveal-on-scroll feature, since the very first callback firing immediately (rather than only on scroll) surprises a lot of people the first time they see it.
  • If you only care about one-time reveal behavior, pair the isIntersecting check with unobserve() rather than removing one and keeping the other — both lines are doing a different, necessary job.

🚨 Why This Is Dangerous

  • Content that’s supposed to lazy-load on scroll but was unobserved before it ever became visible simply never loads, and because there’s no error, it can go unnoticed until a user reports a blank section on the page.

An IntersectionObserver’s first callback isn’t an event — it’s a status report, and status reports happen even when there’s nothing to report yet.

— JavaScript Tip

Related posts:

structuredClone() Beats JSON Hacks

structuredClone() Beats JSON Hacks

JS “Undefined Errors in Loops” — The Closure Trap

JS “Undefined Errors in Loops” — The Closure Trap

JavaScript: Use Map and Set for Efficient Data Structures

JavaScript: Use Map and Set for Efficient Data Structures

Views: 5
Found this useful? Share it
XLinkedInWhatsApp

Post navigation

The AI Prompt That Spots Hidden Fees in a Gym Membership Contract
Photoshop: Fix a Clone Stamp Aligned Mode Resetting Per Stroke

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Most Read

  1. 01How to make theater mode the default for Youtube 1,079 views
  2. 02How to add default value for Entity Framework migrations for DateTime and Bool 996 views
  3. 03Get the User Name and Domain Name from an Email Address in SQL 995 views
  4. 04Get the First and Last Word from a String or Sentence in SQL 873 views
  5. 05How to select distinct rows in a datatable in C# 866 views
  6. 06How to enable, disable and check if Service Broker is enabled on a database in SQL Server 671 views
  7. 07Add Constraint to SQL Table to ensure email contains @ 629 views
  8. 08Average of all values in a column that are not zero in SQL 584 views
  9. 09How to use Map Mode for Vertical Scroll Mode in Visual Studio 561 views
Bits of .NET

Daily micro-tips for C#, SQL, performance, and scalable backend engineering.

Social

  • ErcanOPAK.com
  • GoodReads
  • LetterBoxD
  • Linkedin
  • The Blog
  • Twitter
Back to top
© 2026 Bits of .NET | Built with Xblog Plus free WordPress theme by wpthemespace.com