I would recommend using Lodash for debouncing.
it makes it as easy as:
// ADD THIS
import _ from 'lodash';
import { mapGetters } from 'vuex';
export default {
props: {
gender: {
type: String,
required: true
}
},
computed: {
...mapGetters([
'loadedAthletes'
]),
athleteSearchString: {
get() {
return this.$store.getters.athleteSearchString;
},
set(value) {
this.$store.commit('setAthleteSearchString', value);
}
}
},
methods: {
/**
* Search for athletes
* @return {void}
*/
// UPDATE THIS
search: _.debounce(function() {
axios.post(route('athlete.search'), {
searchString: this.athleteSearchString,
gender: this.gender
}).then((response) => {
if (this.athleteSearchString == '') {
this.$store.commit('setAthletes', this.loadedAthletes);
} else {
this.$store.commit('setAthletes', response.data);
}
}).catch((error) => {
});
}, 200)
// ^ ADD THE TIMEOUT
}
}