debounce

Posted krucecoder

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了debounce相关的知识,希望对你有一定的参考价值。

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的主要内容,如果未能解决你的问题,请参考以下文章