-
-
Notifications
You must be signed in to change notification settings - Fork 191
Open
Description
<script setup>
import { watch, customRef } from "vue"
/**
* Implement the function
*/
function useDebouncedRef(value, delay = 200) {
let timer;
return customRef((track, trigger) => ({
get() {
track()
return value
},
set(newVal, oldVal) {
timer && clearTimeout(timer)
timer = setTimeout(() => {
value = newVal
trigger()
}, delay)
}
}))
}
const text = useDebouncedRef("hello")
/**
* Make sure the callback only gets triggered once when entered multiple times in a certain timeout
*/
watch(text, (value) => {
console.log(value)
})
</script>
<template>
<input v-model="text" />
</template>