Skip to content

Instantly share code, notes, and snippets.

@imbolc
Last active September 2, 2018 04:01
Show Gist options
  • Save imbolc/814cf93e446923a0bfdb7ac36742d40a to your computer and use it in GitHub Desktop.
Save imbolc/814cf93e446923a0bfdb7ac36742d40a to your computer and use it in GitHub Desktop.
Vue debouncing #vuejs #vue.js #debounce #throttle #throttling
<template>
<div v-loading="loading">
<textarea v-model="text"></textarea>
{{ saving ? 'saving...' : '✓' }}
</div>
</template>
<script>
export default {
data: () => ({
text: '',
saving: false,
saveTimer: null,
}),
methods: {
async save () {
this.saving = true
this.saveTimer && clearTimeout(this.saveTimer)
this.saveTimer = setTimeout(() => {
this.$http.post('/some-url', { text: this.text })
this.saving = false
}, 1000)
},
},
watch: {
text (val) { this.save() },
},
}
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment