封装防抖函数
// util.ts
export const debounce = (fn: Function, delay: number) => {
let timer: any;
return function (this: any) {
clearTimeout(timer);
timer = setTimeout(() => {
fn.apply(this, arguments);
}, delay);
};
};
使用防抖
import { debounce } from "~/utils/util";
Page({
...
addCart: debounce(function (this: any) {
this.setDate({
index:1
})
}),
...
});