Vue Debounce Directive

5.0.1 · active · verified Sun Apr 19

vue-debounce is a Vue.js directive that provides a straightforward way to debounce events, primarily user input, within Vue 3 applications. As of its current stable version, 5.0.1, it exclusively supports Vue 3; users requiring Vue 2 compatibility are directed to the separate `vue2-debounce` package or to remain on v4 of vue-debounce. The library is actively maintained, with a cadence of minor and patch releases, and major versions introducing significant architectural shifts such as the transition to ES modules. Its key differentiators include a simple declarative usage model via the `v-debounce` directive, support for various event listeners, customizable debounce times, and several modifiers like `lock`, `fireonempty`, and `trim` for fine-grained control over debouncing behavior. It supports both global application-level registration and component-level usage.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to globally register the `v-debounce` directive in a Vue 3 application, including custom default options. It also shows basic template usage with a specified debounce time and modifiers.

import vueDebounce from 'vue-debounce';
import { createApp } from 'vue';
import App from './App.vue';

const app = createApp(App);

// Register the directive globally with custom options
app
  .directive('debounce', vueDebounce({
    lock: false, // Lock all debounced inputs by default
    defaultTime: '500ms', // Default debounce time for directives without a specified time
    listenTo: ['keyup', 'change'] // Listen to multiple events
  }))
  .mount('#app');

// In your App.vue template:
// <template>
//   <div>
//     <input v-debounce:800ms.trim="mySearchFunction" placeholder="Search..." />
//     <button v-debounce="myClickHandler">Click Me</button>
//   </div>
// </template>

// In your component script:
// export default {
//   methods: {
//     mySearchFunction(value) {
//       console.log('Debounced search for:', value);
//     },
//     myClickHandler() {
//       console.log('Debounced click!');
//     }
//   }
// }

view raw JSON →