v-debounce

Marcos Neves
Jun 26, 2017 · 1 min read

Delay directive for search/filter inputs. Tip #28

For search and filter inputs, I like to delay the execution until the user pauses typing after 500ms, for example, because usually those actions are slow (remote request or processing intensive).

In this post, I explained how to achieve that using lodash.debounce. But as soon as you need for multiple inputs in your app, makes sense to create a custom component, or in this case a directive: v-debounce.

The idea is to capture the input "oninput" event, wait, then dispatch it as another event "onchange", this way I can capture using v-model.lazy.

vuejs-tips

Vue.js Tips and Tricks

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store