1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950 |
- // 防抖
- export const antiShake = (fn, t) => {
- let delay = t || 500;
- let timer;
- return function () {
- let args = arguments;
- if (timer) {
- clearTimeout(timer);
- }
- let callNow = !timer;
- timer = setTimeout(() => {
- timer = null;
- }, delay);
- if (callNow) fn.apply(this, args);
- };
- };
- // 点击防抖
- export const throttle = {
- bind: (el, binding) => {
- let throttleTime = binding.value; // 防抖时间
- if (!throttleTime) {
- // 用户若不设置防抖时间,则默认1s
- throttleTime = 1000;
- }
- let timer;
- let disable = false;
- el.addEventListener(
- "click",
- (event) => {
- if (timer) {
- clearTimeout(timer);
- }
- if (!disable) {
- // 第一次执行(一点击触发当前事件)
- disable = true;
- } else {
- event && event.stopImmediatePropagation();
- }
- timer = setTimeout(() => {
- timer = null;
- disable = false;
- }, throttleTime);
- },
- true
- );
- },
- };
|