Throttle a Scroll or Resize Handler
For scroll-based animations, lazy loading, or resize layouts From the Debouncing & Throttling AI Coding Building Block.
My [app / website] runs a function every time the user scrolls or resizes the window. It's making the page laggy because the function fires hundreds of times per second. Add throttling so the function runs at most once every [200ms / 100ms / 500ms]. Make sure: - The first event fires immediately (so it feels responsive) - The last event also fires (so the final position is captured) - The UI stays smooth during fast scrolling - I can easily adjust the throttle interval later My stack: [your framework and language here] I'm learning, so explain each part simply.