节流、防抖及使用场景

函数防抖(debounce)

概念:在事件被触发n秒后再执行回调,如果在这n秒内又被触发,则重新计时。

//模拟一段ajax请求
function ajax(content) {
    console.log('ajax request ' + content)
}

let inputa = document.getElementById(‘unDebounce’)

inputa.addEventListener(‘keyup’, function (e) {
ajax(e.target.value)
})

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

向input框中一直输入文字:
在这里插入图片描述
只要按下键盘,就会触发ajax请求。这样会增加很多无效的请求浪费资源,实际应用中,是等用户输出完整的字符后才会请求。下面优化一下:

//模拟一段ajax请求
function ajax(content) {
  console.log('ajax request ' + content)
}

function debounce(fun, delay) {
return function (args) {
let that = this
let _args = args
clearTimeout(fun.id)
fun.id = setTimeout(function () {
fun.call(that, _args)
}, delay)
}
}

let inputb = document.getElementById(‘debounce’)

let debounceAjax = debounce(ajax, 500)

inputb.addEventListener(‘keyup’, function (e) {
debounceAjax(e.target.value)
})

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24

优化后减少不必要的请求:
在这里插入图片描述
这里加入了防抖以后,当频繁的输入时,并不会发送请求,只有在指定间隔内没有输入时,才会执行函数。如果停止输入但是在指定间隔内又输入,会重新触发计时。
或者 debounce函数 这么写也行:主要是需要一个id来标识之前的函数

function debounce(fun, delay) {
    let fun_id;
    return function (args) {
        let _args = args;
        let that = this;
        clearTimeout(fun_id);
        fun_id = setTimeout(function () {
            fun.call(that, _args);
        }, delay)
    }
}

 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

函数节流(throttle)

概念:规定在给定时间内,只能触发一次函数。如果在给定时间内触发多次函数,只有一次生效。
栗子:

//模拟一段ajax请求
function ajax(content) {
    console.log('ajax request ' + content)
}
function throttle(fun, delay) {
    let last;
    return function (args) {
        let that = this;
        let _args = args;
        let now = +new Date();
        if (last && last + delay > now) {
            clearTimeout(fun.id);
            fun.id = setTimeout(function () {
                last = now;
                fun.call(that, _args);
            }, delay);
        } else {
            last = now;
            fun.call(that, _args);
        }
    }
}

let throttleAjax = throttle(ajax, 1000);

let inputc = document.getElementById(‘throttle’)

inputc.addEventListener(‘keyup’, function (e) {
throttleAjax(e.target.value)
})

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30

在这里插入图片描述
在不断输入时,ajax会按照我们设定的时间,每1s执行一次。

应用场景:

函数防抖(debounce):

  • search搜索联想,用户在不断输入值时,用防抖来节约请求资源;
  • window触发resize的时候,不断的调整浏览器窗口大小会不断的触发这个事件,用防抖来让其只触发一次

函数节流(throttle):

  • 鼠标不断点击触发,mousedown(单位时间内只触发一次)
  • 监听滚动事件,比如是否滑到底部自动加载更多,用throttle来判断

总结:

  • 函数防抖和函数节流都是防止某一时间频繁触发,但原理不一样。
  • 函数防抖是某一段时间内只执行一次,而函数节流是间隔时间执行。
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值