js防抖和节流

本文详细介绍了JavaScript中的防抖(debounce)和节流(throttle)技术,通过实例代码展示了它们的工作原理和应用场景。防抖技术用于限制函数的执行次数,确保在特定时间间隔后才执行;节流则用来降低函数执行频率,保证在一段时间内只执行一次。文中还提供了封装版的防抖和节流函数,并应用于输入框的keyup事件和拖动元素的ondrag事件中。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <!-- 
        防抖和节流都是限制函数的执行次数

        防抖:
            概念:通过setTimeout方法,在一定时间间隔内,将多次触发变成一次触发
            实现原理:触发高频事件后n秒内函数只会执行一次,如果n秒内高频事件再次被触发,则重新计算时间。
        节流:
            概念:减少一段时间的触发频率
            实现原理:高频事件触发,但在n秒内只会执行一次,所以节流会稀释函数的执行频率。
    -->

    <!-- 防抖 -->
    <input type="text" id="input1">
    <br>
    <input type="text" id="input2">
    <script>
        // 介绍版
        const input1 = document.querySelector("#input1");
        let timer = null;
        input1.onkeyup = () => {
            if (timer) {
                clearTimeout(timer);
            }
            timer = setTimeout(() => {
                console.log(input1.value);
            }, 500)
        }

        // 封装版
        let debounce = (fn, delay = 300) => {
            let timer;
            return function () {
                if (timer) {
                    clearTimeout(timer);
                }
                timer = setTimeout(() => {
                    fn();
                }, delay)
            }
        }
        let ajax = () => {
            console.log('服务器响应');
        }
        const input2 = document.querySelector("#input2");
        input2.onkeyup = debounce(ajax, 500);
    </script>

    <!-- 节流 -->
    <style>
        #box1,#box2 {
            width: 100px;
            height: 100px;
        }
        #box1{background-color: royalblue;}
        #box2{background-color: red;}
    </style>
    <div id="box1" draggable="true"></div>
    <div id="box2" draggable="true"></div>
    <script>
        // 介绍版
        const box1 = document.querySelector('#box1');
        let time = null;
        box1.ondrag = (e) => {
            if (time) {
                return;
            }
            time = setTimeout(() => {
                console.log(e.offsetX, e.offsetY);  // 火狐不兼容
                time = null;
            }, 300)
        }

        // 封装版 
        let throttle = (fn,delay) => {
            let timer;
            return function(){
                if(timer){
                    return;
                }
                timer = setTimeout(() => {
                    fn();
                    timer = null;
                },delay)
            }
        }
        let response = () => {
            console.log('节流');
        } 
        const box2 = document.querySelector('#box2');
        box2.ondrag = throttle(throttle(response),300);
    </script>
</body>

</html>

更多相关内容大家可以前往我的个人博客浏览:eyes++的个人空间

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值