美文网首页ts
装饰器 - 节流

装饰器 - 节流

作者: johnnie_wang | 来源:发表于2020-07-04 17:27 被阅读0次
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

复制直接使用~

相关文章

  • 装饰器 - 节流

    Throttle 使用说明: 先引入import { DRThrottle } from '@/utils/dec...

  • io

    io流用的是装饰者模式 字节流 inputsteam是组件接口,fliterinputsteam是装饰器的抽象父类...

  • TS 防抖节流装饰器

  • 装饰器

    """@装饰器- 普通装饰器- 带参数的装饰器- 通用装饰器- 装饰器装饰类- 内置装饰器- 缓存装饰器- 类实现...

  • typescript 五种装饰器

    装饰器类型 装饰器的类型有:类装饰器、访问器装饰器、属性装饰器、方法装饰器、参数装饰器,但是没有函数装饰器(fun...

  • python——装饰器详解

    一、装饰器概念 1、装饰器 装饰器:一种返回值也是一个函数的函数,即装饰器。 2、装饰器目的 装饰器的目的:装饰器...

  • Python装饰器

    Python装饰器 一、函数装饰器 1.无参装饰器 示例:日志记录装饰器 2.带参装饰器 示例: 二、类装饰器 示例:

  • Python中的装饰器

    Python中的装饰器 不带参数的装饰器 带参数的装饰器 类装饰器 functools.wraps 使用装饰器极大...

  • 装饰器

    装饰器 decorator类装饰器 带参数的装饰器 举例(装饰器函数;装饰器类;有参与无参) https://fo...

  • TypeScript装饰器

    前言 装饰器分类 类装饰器 属性装饰器 方法装饰器 参数装饰器需要在tsconfig.json中启用experim...

网友评论

    本文标题:装饰器 - 节流

    本文链接:https://www.haomeiwen.com/subject/trouqktx.html