mediate-manage-web/src/utils/clickThrottle.js
2024-11-29 16:04:56 +08:00

36 lines
1.1 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/* 防止重复点击 */
let clickTimer = 0
function clickThrottle(interval = 3000) {
let now = +new Date(); // 获取当前时间的时间戳
let timer = clickTimer; // 记录触发事件的事件戳
if (now - timer < interval) {
// 如果当前时间 - 触发事件时的事件 < interVal那么不符合条件直接return false
// 不让当前事件继续执行下去
return false;
} else {
// 反之,记录符合条件触发了事件的时间戳,并 return true使事件继续往下执行
clickTimer = now;
return true;
}
}
// 使用方式1使用防重复点击事件默认参数值30003秒内该事件不会继续执行下去
// 这是一个提交事件
// handleSubmit() {
// if(!clickThrottle()) return;
// // doing
// // 调用提交保存接口
// }
// 使用方式2修改防重复点击事件默认参数值5秒内该事件不会继续执行下去
// 这是一个提交事件
// handleSubmit() {
// if(!clickThrottle(5000)) return;
// // doing
// // 调用提交保存接口
// }
export default clickThrottle