Throttle a Function
Throttle a Function is a reusable JavaScript snippet. Creates a throttled version of a function that executes at most once per `interval` milliseconds. Useful for scroll and resize events. The full implementation: function throttle(fn, interval = 100) { let lastTime = 0 return function(...args) { const now = Date.now() if (now - lastTime >= interval) { lastTime = now fn.apply(this, args) } } } // Usage window.addEventListener('scroll', throttle(() => { console.log('Scrolled!') }, 200)) This snippet uses only standard JavaScript with no dependencies, so it runs in modern browsers and Node.js alike. Browse more patterns in the snippet library, or paste the code straight into the playground to experiment with it.