1.封装
shake: function (func,delay = 3000) {
const { timer } = this.data;
if (timer) clearTimeout(timer);
this.data.timer = setTimeout(() => func(), delay);
},
function throttling(func, delay = 3000) {
let { isDisable } = this.data;
if (!isDisable) {
this.data.isDisable = true;
setTimeout(() => {
this.data.isDisable = false;
}, delay);
try {
func();
} catch (error) {
console.log("error :>> ", error);
}
}
}
async throttlingPromiseComplete(func) {
let { isDisable } = this.data
if (!isDisable) {
this.data.isDisable = true;
try {
await func();
} catch (error) {
console.log("error :>> ", error);
} finally {
this.data.isDisable = false;
}
}
},
throttlingComplete(func) {
let { isDisable } = this.data
if (!isDisable) {
this.data.isDisable = true;
try {
func();
} catch (error) {
console.log("error :>> ", error);
} finally {
this.data.isDisable = false;
}
}
},
2.调用
shake(()=>{
},200)
throttling(()=>{
},200)
throttlingPromiseComplete(async ()=>{
},200)
throttlingComplete(()=>{
},200)