zoukankan      html  css  js  c++  java
  • js防抖

    那我们设置个 options 作为第三个参数,然后根据传的值判断到底哪种效果,我们约定:

    leading:false 表示禁用第一次执行
    trailing: false 表示禁用停止触发的回调

    我们来改一下代码:

    // 第四版
    function throttle(func, wait, options) {
        var timeout, context, args, result;
        var previous = 0;
        if (!options) options = {};
    
        var later = function() {
            previous = options.leading === false ? 0 : new Date().getTime();
            timeout = null;
            func.apply(context, args);
            if (!timeout) context = args = null;
        };
    
        var throttled = function() {
            var now = new Date().getTime();
            if (!previous && options.leading === false) previous = now;
            var remaining = wait - (now - previous);
            context = this;
            args = arguments;
            if (remaining <= 0 || remaining > wait) {
                if (timeout) {
                    clearTimeout(timeout);
                    timeout = null;
                }
                previous = now;
                func.apply(context, args);
                if (!timeout) context = args = null;
            } else if (!timeout && options.trailing !== false) {
                timeout = setTimeout(later, remaining);
            }
        };
        return throttled;
    }




    https://github.com/mqyqingfeng/Blog/issues/26
  • 相关阅读:
    2021冬季学期有感与评价
    Scala变量和数据类型
    Scala运算符
    pytorch基本使用
    Vue组件
    Scala函数式编程
    Python中的运算符
    Scala流程控制
    Scala环境搭建
    用于测试GRPC服务的UI工具BloomRpc
  • 原文地址:https://www.cnblogs.com/qdcnbj/p/9945333.html
Copyright © 2011-2022 走看看