节流防抖

概要

防抖:先放你走,如果有新的人来了,再把你叫回来。(先用setTimeout让函数在一段时间后执行,如果有新的事件进来了,就杀死之前的timer),特点是杀死触发的事件。


节流:你是动物园的饲养员,有很多人要给兔子喂胡萝卜(可能争先恐后的哦),你的策略是等吃完了上一根才允许下一个人继续给胡萝卜。(当已经在setTimeout执行了一个函数,就把开关关闭。函数执行完之后,记得把开关打开。)特点是杀死触发的事件。


防抖demo

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        #box {
            background: #ccc;
            width: 1000px;
            height: 500px;
            text-align: center;
            line-height: 500px;
            font-size: 5rem;

        }
    </style>
</head>

<body>
    <div id="box"></div>
    <script>
        const debounce =(func, delay)=> {
            let timer = null;
            return function(...rest){
                clearTimeout(timer);
                timer = setTimeout(() => {
                    func.apply(this, rest);
                }, delay);
            }
        }
        document.getElementById("box").onmousemove = debounce(function (e) {
            this.innerHTML = `${e.clientX}-${e.clientY}`
        }, 50)
    </script>
</body>

</html>

节流demo

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        #box {
            background: #ccc;
            width: 1000px;
            height: 500px;
            text-align: center;
            line-height: 500px;
            font-size: 5rem;

        }
    </style>
</head>

<body>
    <div id="box"></div>
    <script>
        function throttle(func, delay) {
            let flag = true;
            return function(...rest) {
                if(!flag){
                    return ;
                }
                flag = false; 
                setTimeout(() =>{
                    func.apply(this,rest);
                    flag = true;
                },delay);
            }
        }
        document.getElementById("box").onmousemove = throttle(function (e) {
            this.innerHTML = `${e.clientX}-${e.clientY}`
        }, 500)
    </script>
</body>

</html>

综合版

<!DOCTYPE html>
<html lang="en">
 
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        #box {
            background: #ccc;
            width: 1000px;
            height: 500px;
            text-align: center;
            line-height: 500px;
            font-size: 5rem;
 
        }
    </style>
</head>
 
<body>
    <select id="select">
        <option value="audi">防抖</option>
        <option value="volvo">节流</option>
        <option value="mst">不加入防抖和节流</option>
    </select>
    <div id="box"></div>
    <script>
        const debounce = (func, delay) => {
            console.log('debounce');
            let timer = null;
            return function (...rest) {
                clearTimeout(timer);
                timer = setTimeout(() => {
                    func.apply(this, rest)
                }, delay);
            }
        }
        function throttle(func, delay) {
            console.log('throttle');
            let flag = true;
            return function (...rest) {
                if (flag) {
                    flag = false;
                    setTimeout(() => {
                        func.apply(this, rest);
                        flag = true;
                    }, delay);
                }
            }
        }
        function pure(func){
            return function(...rest){
                func.apply(this,rest);
            }
        }

        const option = [debounce, throttle,pure];
 
        // 业务
        var box = document.getElementById("box");
        var myselect = document.getElementById("select");
        var index = myselect.selectedIndex;
 
        const main = () => {
            box.onmousemove = option[index](function (e) {
                this.innerHTML = `${e.clientX}-${e.clientY}`
            }, 50)
        }
 
        myselect.addEventListener('change', function () {
            index = myselect.selectedIndex;
            main();
        }, false)
        main();
    </script>
</body>
 
</html>

tips

@wx:qq981145483

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值