美文网首页
防抖与截流

防抖与截流

作者: EmilWong | 来源:发表于2019-07-30 14:29 被阅读0次
    function debounce (fn, wait, immediate) {
        let timer = null;
        return function () {
            let args = arguments, context = this;
            if (immediate && !timmer) {
                fn.apply(context, args)
            }
            if (timer) clearTimeout(timer)
            timer = setTimeout(()=>{
                fn.apply(context, args);
            }, wait);
        }
    } 
    function throttle(fn, wait, immediate) {
        let timer = null, callNow = immediate;
        return function () {
            let args = arguments, context = this;
            if (callNow) {
                fn.apply(context, args);
                callNow = false;
            }
            if (!timer) {
                timer = setTimeout(() => {
                    fn.apply(context, args);
                    timer = null;
                }, wait);
            }
        }
    }
    

    相关文章

      网友评论

          本文标题:防抖与截流

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