节流(Throttling )和防抖(Debouncing )
来源:互联网 发布:网狐精华版 源码 编辑:程序博客网 时间:2024/04/30 11:05
为了区分这两个思想,最常用的例子就是电梯例子,我们也通过该例子来比较两个思想。当有很多人用电梯时,我们不能每来一个人,电梯都特意去送这个人一趟。这样,电梯的运行方式可以有两种优化思想
debouncing
每上一个人,从这个人开始,指定时间(比如400ms)后电梯关闭开始送人。在程序中,也就是每发生一个事件,从这个事件往后T秒,执行回调函数。
//实现函数:var debounce = function(func, delay) {var inDebounce = undefined;return function() {debuggervar context = this,args = arguments;clearTimeout(inDebounce);return inDebounce = setTimeout(function() {return func.apply(context, args);}, delay);}}
<!-- 调用函数 --><body><button >click me</button></body><script>function aFunction(msg){console.log(msg);}$('button').click(debounce(function() {return aFunction('i am message');}, 400));</script>
Throttling
从上第一个人开始,电梯指定时间循环着执行,无论中间是否上人,都是每t秒运行一次。
//实现函数:var throttle = function(func, limit) {var inThrottle = undefined;return function() {var args = arguments,context = this;if (!inThrottle) {func.apply(context, args);inThrottle = true;return setTimeout(function() {return inThrottle = false;}, limit);}};};
<!-- 调用函数 --><body><button >click me</button></body><script>function aFunction(msg){console.log(msg);}$('button').click(throttle(function() {return aFunction('i am message');}, 400));</script>
0 0
- 节流(Throttling )和防抖(Debouncing )
- 实例解析防抖动(Debouncing)和节流阀(Throttling)(前端,app)
- 节流和防抖
- JavaScript小知识点(二):函数防抖和节流
- [JS]防抖和节流
- [JS]防抖和节流
- 函数防抖和节流
- 页面优化之高性能滚动scroll(防抖和节流)
- debounce 防抖和 throttle 节流
- 函数防抖和函数节流
- 频繁操作中的防抖和节流
- 函数节流与防抖(参考lodash)
- 云计算设计模式(二十三)——Throttling节流模式
- 函数节流和防抖动
- 防抖函数和节流函数小列
- 函数节流与防抖
- 函数防抖与节流
- 函数的节流 & 防抖
- 火车收集算法及其优点
- 《代码大全》读书笔记
- 指针,数组,函数,三者的恩恩怨怨
- Android 7.1 引入了对框架 ExifInterface 的重大改进
- Java总结篇系列:Java多线程(三)
- 节流(Throttling )和防抖(Debouncing )
- 字符串的排列
- [Codeforces551E]GukiZ and GukiZiana(分块)
- 深入理解 RocketMQ
- No parameter name specified for argument of type [java.lang.String],
- Python的切片操作
- Unix信号机制(上)
- 线性模型(三)--ridge、lasso、ElasticNet回归
- 【理论】深入学习spring boot