码迷,mamicode.com
首页 > 其他好文 > 详细

debounce

时间:2019-07-05 19:27:13      阅读:104      评论:0      收藏:0      [点我收藏+]

标签:OLE   wait   this   rip   time   nbsp   nal   console   int()   

function Debounce(wait: number, immediate: boolean = false) {
    return function (target: any, propertyKey: string | symbol, descriptor: PropertyDescriptor) {
        let timeout: any;
        const originalMethod = descriptor.value;
        descriptor.value = function (...args: any[]) {
            let context = this;
            let later = function () {
                timeout = null;
                if (!immediate) {
                    originalMethod.apply(context, args)
                }
            }
            let callNow = immediate && !timeout;
            clearTimeout(timeout)
            timeout = setTimeout(later, wait);
            if (callNow) {
                originalMethod.apply(context, args)
            }
        }

        return descriptor;
    }
}


class MouseObj {

    @Debounce(1000)
    public print() {
        console.log(1)
    }
}


(window as any)["MouseObj"] = MouseObj

 

debounce

标签:OLE   wait   this   rip   time   nbsp   nal   console   int()   

原文地址:https://www.cnblogs.com/KruceCoder/p/11140093.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!