I am trying to create a reusable debounce function in Vue 3 using the composition API but am struggling to get it to work.
This is what I have so far:
debounce.js
const debounce = (fn, delay) => {
let timeout
return (...args) => {
if (timeout) {
clearTimeout(timeout)
}
timeout = setTimeout(() => {
fn(...args)
}, delay)
}
}
export default debounce
BaseComponent.vue
<template>
<input type="text" @input="onInput" />
</template>
<script>
import debounce from './debounce'
export default {
setup() {
const onInput = () => {
debounce(() => {
console.log('debug')
}, 500)
}
return { onInput }
}
}
</script>
The callback passed to the debounce function is not being triggered. I'm not seeing the output in the console.