The Vue.js docs suggest using lodash's debounce function to debounce expensive methods, which I have successfully implemented. But occaisionally I don't want to act immediately, and lodash's docs say:
The debounced function comes with a cancel method to cancel delayed func invocations and a flush method to immediately invoke them.
But that is it. No information how to invoke the flush method. I found this blog post, but I am not sure how to implement that in a Vue.js component.
This is what I have in my Vue.js component at the moment (codepen):
<template>
<input type='text' @keyup="change" @keyup.enter="changeNow">
</template>
<script>
export default {
name: "MyComponent",
methods: {
change: _.debounce(function() {
console.log("changing...");
}, 250),
changeNow: function() {
this.change();
this.change.flush();
}
}
};
</script>
change is properly debounced and only run once after typing, as expected. However, changeNow throws the following error:
Uncaught TypeError: this.change.flush is not a function
Any advice on how to implement this would be greatly appreciated!