CoreUI ONE — 250+ PRO components and 25+ admin templates for React, Angular, Vue, Bootstrap and Next.js, plus Data Grid, Scheduler, 4,300+ PRO icons and the Next.js boilerplate. One license. → See what you get

How to debounce a function in JavaScript

Debouncing prevents excessive function calls by delaying execution until after a specified period of inactivity. As the creator of CoreUI, I’ve implemented debouncing extensively in search inputs, resize handlers, and user interaction events. From my expertise, the most effective approach is creating a closure that manages a timer, clearing previous timeouts when new calls occur. This technique dramatically improves performance by ensuring functions execute only after user activity stops.

Create a debounce function that delays execution until after a specified period of inactivity.

function debounce(func, delay) {
  let timeoutId
  return function(...args) {
    clearTimeout(timeoutId)
    timeoutId = setTimeout(() => func.apply(this, args), delay)
  }
}

Here the debounce function returns a new function that manages a timeoutId variable through closure. When called, it clears any existing timeout with clearTimeout(timeoutId) and sets a new one with setTimeout(). The function only executes after the specified delay period passes without additional calls. The func.apply(this, args) ensures the original function receives the correct context and arguments.

Best Practice Note:

This is the same approach we use in CoreUI components for search inputs and scroll event optimization. Debouncing is ideal for events that fire rapidly like keystrokes, window resizing, or API calls triggered by user input.


Speed up your responsive apps and websites with fully-featured, ready-to-use open-source admin panel templates—free to use and built for efficiency.


About the Author

Subscribe to our newsletter
Get early information about new products, product updates and blog posts.
JavaScript printf equivalent
JavaScript printf equivalent

How to Open All Links in New Tab Using JavaScript
How to Open All Links in New Tab Using JavaScript

How to Remove Underline from Link in CSS
How to Remove Underline from Link in CSS

Understanding and Resolving the “React Must Be in Scope When Using JSX
Understanding and Resolving the “React Must Be in Scope When Using JSX