Skip to content

Instantly share code, notes, and snippets.

@adriandmitroca
Created December 18, 2018 11:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save adriandmitroca/1da0a8f3b9f12c9aba0f54a4f85debdf to your computer and use it in GitHub Desktop.
Save adriandmitroca/1da0a8f3b9f12c9aba0f54a4f85debdf to your computer and use it in GitHub Desktop.
Debounce done right in Vue.js
import debounce from 'lodash/debounce';
export default {
data() {
return {
loading: false,
query: null,
};
},
watch: {
query() {
this.debouncedFetch();
},
},
created() {
this.debouncedFetch = debounce(this.fetch, 500);
},
mounted() {
this.fetch();
},
methods: {
async fetch() {
try {
this.loading = true;
const { data } = await window.axios.post(this.fetchUrl, { query: this.query });
} catch (e) {
console.error(e);
}
this.loading = false;
},
},
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment