Home / Snippets / Vue Js Prevent Methods and Events From Spamming Using Debounce
Vue Js Prevent Methods and Events From Spamming Using Debounce cover

Vue Js Prevent Methods and Events From Spamming Using Debounce

429

3 years ago

0 comments

To prevent your Vue Js methods and events from mass spamming / calling too often, you can make use of Lodash's "debounce()" function.

Vue Js debounce Code Example


Below is how you can define the "debounce()" function within your method function body. For this code example, the debounce will execute the function to perform the ajax every 500ms after the input has changed.
<template>
  <input
    type="search"
    v-on:input="filter"
    placeholder="Search..."
  >
</template>

<script>
import { debounce } from 'lodash';

export default {
  methods: {
    filter: debounce(function () {
      // your search ajax here
    }, 500)
  }
}
</script>
notion avatar

Alaz

Week-end developer currently experimenting with web, mobile, and all things programming.

Topics:

Frontend

Resource

Average

Average

Support Us

If you like our tutorial, support us by being our Patreon or buy us some coffee ☕️

Welcome to PostSrc V3

PostSrc Dark Logo

You have to login to favorite this