前端学习(七)----防抖节流

防抖:多次触发某一个事件只关注第一次或最后一次

节流:多次触发时间保证一定时间内一定产生一次结果

防抖立即执行版本 


<body>
    <style>
        div {
            height: 200px;
            width: 200px;
            background-color: red;
        }
    </style>
    </head>

    <body>
        <div id="d1"></div>
        <script>
            //立即执行版本

            // var d1 = document.getElementById('d1');
            // var handle = function(e) {
            //     console.log(100)
            // }

            // function debounce(func, wait) { //防抖函数
            //     var timeout = null;
            //     return function() {
            //         var context = this;
            //         var arg = arguments; //event   事件e参数 点击事件会有一个e参数
            //         if (timeout) {
            //             clearTimeout(timeout)
            //         }
            //         var callNow = !timeout;
            //         timeout = setTimeout(function() {
            //             timeout = null
            //         }, wait)
            //         if (callNow) {
            //             func.apply(context, arg)
            //         }
            //     }
            // }
            // d1.addEventListener('click', debounce(handle, 1000))
        </script>
</body>

 防抖延迟执行版本


<body>
    <style>
        div {
            height: 200px;
            width: 200px;
            background-color: red;
        }
    </style>
    </head>

    <body>
        <div id="d1"></div>
        <script>

            // var d1 = document.getElementById('d1');
            // var handle = function(e) {
            //     console.log(100)
            // }

            // var d1 = document.getElementById('d1')
            // var handle = function() {
            //     console.log(1)
            // }

            // function debounce(func, wait) {
            //     var timeout = null
            //     return function() {
            //         var content = this
            //         var args = arguments
            //         if (timeout) {
            //             clearTimeout(timeout)
            //         }
            //         timeout = setTimeout(function() {
            //             func.apply(content, args)
            //         }, wait)
            //     }

            // }
            // d1.addEventListener('click', debounce(handle, 1000))
        </script>
</body>

 时间戳版本节流(立即执行版本)

<body>
    <div id="d1"></div>
    <script>
        var d1 = document.getElementById('d1')

        function handle() {
            console.log(1)
        }

        function throttle(func, wait) {
            var previous = 0
            return function() {
                var now = Date.now()
                var content = this
                var args = arguments
                if (now - previous > wait) {
                    func.apply(content, args)
                    previous = now
                }
            }
        }
        d1.addEventListener('click', throttle(handle, 1000))
    </script>
</body>

延迟执行版本

<body>
    <div id="d1"></div>
    <script>
        var d1 = document.getElementById('d1')

        function handle() {
            console.log(1)
        }

        function throttle(func, wait) {
            var timeout = null
            return function() {
                var content = this
                var args = arguments
                if (!timeout) {
                    timeout = setTimeout(() => {
                        timeout = null
                        func.apply(content, args)
                    }, wait)
                }
            }
        }
        d1.addEventListener('click', throttle(handle, 1000))

    </script>
</body>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值