vue-hooks-plus
Version:
Vue hooks library
28 lines (27 loc) • 608 B
JavaScript
import throttle from "lodash-es/throttle";
import { ref, computed, onUnmounted } from "vue";
function useThrottleFn(fn, options) {
var _a;
const fnRef = ref(fn);
const wait = (_a = options == null ? void 0 : options.wait) != null ? _a : 1e3;
const throttled = computed(
() => throttle(
(...args) => {
return fnRef.value([...args]);
},
wait,
options
)
);
onUnmounted(() => {
throttled.value.cancel();
});
return {
run: throttled,
cancel: throttled.value.cancel,
flush: throttled.value.flush
};
}
export {
useThrottleFn as default
};