说明:
防抖:n 秒后在执行该事件,若在 n 秒内被重复触发,则重新计时
// 防抖
const res = _.debounce(this.handleConsole, 2000);
节流:n 秒内只运行一次,若在 n 秒内重复触发,只有一次生效
// 节流
const res1 = _.throttle(this.handleConsole, 2000);
handleClick = () => {
// 首次点击时执行,连续点击且时间间隔在1s之内,不再执行,间隔在1s之外再次点击,执行。
this.debounce();
// 首次点击时执行,连续点击且事件间隔在1s之内,1s之后自动执行一次(注:连续点击次数时间之后小于1s,则不会自动执行),间隔在1s之外再次点击,执行。
this.throttle();
}
debounce = _.debounce(this.handleConsole, 1000, {
leading: true,
trailing: false
});
throttle = _.throttle(this.handleConsole, 1000, {
leading: true,
trailing: false,
});
// 不能使用箭头函数,会报错
handleConsole () {
console.log(111);
}
用法:
// 防抖:n 秒后在执行该事件,若在 n 秒内被重复触发,则重新计时
const handleSelectChange = _.debounce(
async item => {
const type = item.key;
// 不存在数字人不跳转界面
const __loginInfo = Taro.getStorageSync('__loginInfo');
const [_, resp] = await promiseTo(designServices.fetchMallList({customerId: __loginInfo.pcInfo.customerId, compId: __loginInfo.pcInfo.compId}));
if (!resp.bean || !resp.bean.digitalHumanId) {
return showToast('mall-sensitive-word', 'fail', `请联系客服购买数字人`);
}
const curVideoInfo = await handleAddAudioInfo(type);
if (curVideoInfo) {
Taro.navigateTo({
url: `/mod-design/pages/video-creator/index?type=${type}&curVideoInfo=${encodeURIComponent(
JSON.stringify(curVideoInfo)
)}&creationType=${''}&from=detect&detectContent=${encodeURIComponent(editorContent ? editorContent.trim() : '')}`
});
}
handleHideTooltip();
},
3000,
{leading: true, trailing: false}
);