装饰器 - 节流

export const DRThrottle = (delay: number) => (targetL:any, key:any, descriptor:any) => {
  let last: any
  let deferTimer: any
  const original = descriptor.value
  descriptor.value = function() {
    const now = +new Date()
    if (!last || !(now < last + delay)) {
      last = now
      original.apply(this, arguments)
    } else {
      this.$message.error('请勿重复提交')
    }
  }
  return descriptor
}

Throttle

使用说明:

  1. 先引入

    import { DRThrottle } from '@/utils/decorators'
    
  2. 使用 @DRThrottle 装饰你的事件回调函数,参数为:多少ms内只执行一次

    @DRThrottle(5000)
    foo(){
    ···
    }
    
  3. 约定节流时间 设定为5000ms, 接口请求超时为10000ms

复制直接使用~

你可能感兴趣的:(装饰器 - 节流)