Created
August 5, 2020 00:34
-
-
Save toast38coza/ef4252461d49f87fa8670794ecf5d2b2 to your computer and use it in GitHub Desktop.
A simple way to do a debounce with Vue (not dependencies required)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<template> | |
<input type=text v-model='value' @kepup='debounceExpensiveMethod' /> | |
</template> | |
<script> | |
export default { | |
name: 'SomeComponent', | |
data () { | |
return { | |
timeout: null | |
} | |
}, | |
methods: { | |
debounceExpensiveMethod () { | |
if (this.timeout) { | |
window.clearTimeout(this.timeout) | |
} | |
this.timeout = window.setTimeout(this.expensiveMethod, 1000) | |
}, | |
expensiveMethod () { | |
} | |
} | |
} | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment