开发者社区 问答 正文

实现一个debounce装饰器

实现一个debounce装饰器

展开
收起
景凌凯 2020-03-25 22:45:25 817 分享
分享
版权
举报
1 条回答
写回答
取消 提交回答
  • 有点尴尬唉 你要寻找的东西已经被吃掉啦!

    /** * 装饰器的debounce * @param delay */ export function debounce(delay: number): Function { return ( target: Function, propertyKey: string, propertyDesciptor: PropertyDescriptor ) => { const method = propertyDesciptor.value; let timer = null; propertyDesciptor.value = (...args) => { if (timer) { clearTimeout(timer); timer = null; } timer = setTimeout(() => method(...args), delay); }; return propertyDesciptor; }; }

    2020-03-25 22:45:34 举报
    赞同 评论

    评论

    全部评论 (0)

    登录后可评论
问答地址: