装饰器 - 节流

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

复制直接使用~

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。