我在vue3中使用了可组合的去盎司函数,而不是mixin。但它不会像预期的那样工作(取消超时函数调用)。它是清除超时,当取消公告的函数调用cleartimeout时,timeouted函数立即调用。
export default function useDebounceMethod() {
const debounce = function(fn, interval) {
let timer;
return function debounced() {
clearTimeout(timer);
let args = arguments;
let that = this;
timer = setTimeout(() => {
fn.apply(that, args);
}, interval);
};
}
return {
debounce
}
}
然后像这样使用:
import useDebounceMethod from "@/composables/mixin";
export default {
name: "Search",
components: {},
setup() {
// only instead of mixins
const { debounce } = useDebounceMethod();
return {
debounce,
};
},
data() {
const dsFunction = this.debounce(function () {
this.setRepositories();
}, 1000);
return {
debouncedSearch: dsFunction,
};
},
...
稍后致电:
this.debouncedSearch()
暂无答案!
目前还没有任何答案,快来回答吧!