18 lines
413 B
JavaScript
18 lines
413 B
JavaScript
import { watch, isRef } from 'vue';
|
|
|
|
export default function useRefChange(data, cb, delay = 750) {
|
|
if (!isRef(data) || typeof cb !== 'function' || typeof delay !== 'number' || !(delay > 0)) return;
|
|
let timer = null;
|
|
|
|
function chang() {
|
|
if (timer) {
|
|
clearTimeout(timer);
|
|
}
|
|
timer = setTimeout(() => {
|
|
cb(data.value);
|
|
}, delay);
|
|
}
|
|
|
|
watch(data, () => chang(), { deep: true });
|
|
|
|
} |