js 消抖(debounce)与节流(throttle)
前言
故事发生在与大创的又一次撕逼(日常)中,我方坚定的认为:作为社会主义接班人,节流与消抖的界限是明显的,是不容混肴的,是不可侵犯的!对方辩友坚持地觉得:界限是模糊的,行为是暧昧的,性别是可以忽视的(。。。)。本着凡事要往祖坟上刨的精神,对这两个概念进行了一番深刻的社会主义改造。
定义
节流(throttle)
定义: 如果一个函数持续的,频繁地触发,那么让它在一定的时间间隔后再触发。
感觉像是去排队过安检,当人很多的时候(持续地要进门),安保会隔一段时间放进去几个进行安检(一定时间的间隔)。
类似这种的feel

消抖(debounce)
定义: 如果一个函数持续地触发,那么只在它结束后过一段时间只执行一次。
像是两个人的对话,A在不停的balabala(持续触发),如果他说话的时候有停顿(一定间隔),但是停顿的时间不够长,就认为A没有说完, 当停顿时间超过一某个范围就认为A说完了,然后B开始回答(响应)。
场景
前提其实都是某个行为持续地触发,不同之处只要判断是要优化到减少它的执行次数还是只执行一次就行。
举个例子,像dom的拖拽,如果用消抖的话,就会出现卡顿的感觉,因为只在停止的时候执行了一次,这个时候就应该用节流,在一定时间内多次执行,会流畅很多。
但是,如果是输入联想这种,我要输入“谁是世界上最帅的人?”,多次执行的话,可能当我输到最的时候,就去搜索,结果搜出来一堆“谁是世界上最笨”,“谁是世界上最胖”之类的不必要搜索,只需要在输入完成后进行搜索,消抖当时最合适啦。
实现
明白了定义与场景,让我们 脱下裤子 撸起袖子来造一个。
先看看名家手笔
underscore的实现
// Returns a function, that, as long as it continues to be invoked, will not
// be triggered. The function will be called after it stops being called for
// N milliseconds. If `immediate` is passed, trigger the function on the
// leading edge, instead of the trailing.
_.debounce = function(func, wait, immediate) {
var timeout, result; var later = function(context, args) {
timeout = null;
if (args) result = func.apply(context, args);
}; var debounced = restArgs(function(args) {
if (timeout) clearTimeout(timeout);
if (immediate) {
var callNow = !timeout;
timeout = setTimeout(later, wait);
if (callNow) result = func.apply(this, args);
} else {
timeout = _.delay(later, wait, this, args);
} return result;
}); debounced.cancel = function() {
clearTimeout(timeout);
timeout = null;
}; return debounced;
};
// Returns a function, that, when invoked, will only be triggered at most once
// during a given window of time. Normally, the throttled function will run
// as much as it can, without ever going more than once per `wait` duration;
// but if you'd like to disable the execution on the leading edge, pass
// `{leading: false}`. To disable execution on the trailing edge, ditto.
_.throttle = function(func, wait, options) {
var timeout, context, args, result;
var previous = 0;
if (!options) options = {}; var later = function() {
previous = options.leading === false ? 0 : _.now();
timeout = null;
result = func.apply(context, args);
if (!timeout) context = args = null; //显示地释放内存,防止内存泄漏
}; var throttled = function() {
var now = _.now();
if (!previous && options.leading === false) previous = now;
var remaining = wait - (now - previous);
context = this;
args = arguments;
if (remaining <= 0 || remaining > wait) {
if (timeout) {
clearTimeout(timeout);
timeout = null;
}
previous = now;
result = func.apply(context, args);
if (!timeout) context = args = null;
} else if (!timeout && options.trailing !== false) {
timeout = setTimeout(later, remaining);
}
return result;
}; throttled.cancel = function() {
clearTimeout(timeout);
previous = 0;
timeout = context = args = null;
}; return throttled;
};
实现了更灵活的option去控制是否立即执行,是否劫数是在执行以及增加cancel的控制逻辑。本着大道至简的精神(其实是懒),我们来改一个精简版只实现基本业务。
function debounce (fn, delay) {
let timer = null;
return function () {
let args = arguments;
let context = this;
if (timer) {
clearTimeout(timer);
timer = setTimeout(function () {
fn.apply(context, args);
}, delay);
} else {
timer = setTimeout(function () {
fn.apply(context, args);
}, delay);
}
}
}
function throttle (fn, delay) {
let timer = null,
remaining = 0,
previous = new Date();
return function () {
let now = new Date(),
remaining = now - previous,
args = arguments,
context = this;
if (remaining >= delay) {
if (timer) {
clearTimeout(timer);
}
fn.apply(context, args);
previous = now;
} else {
if (!timer) {
timer = setTimeout(function () {
fn.apply(context, args);
previous = new Date();
}, delay - remaining);
}
}
};
}
吐槽
红宝书上给的节流例子,嗯。。。怎么说呢。。。 是非常棒的消抖的实现,谢谢!!!!!
2018/02/08 throttle与debounce的可视化比较
js 消抖(debounce)与节流(throttle)的更多相关文章
- 防抖debounce和节流throttle
大纲 一.出现缘由 二.什么是防抖debounce和节流throttle 三.应用场景 3.1防抖 3.2节流 一.出现缘由 前端开发中,有一部分用户行为会频繁触发事件,而对于DOM操作,资源加载等耗 ...
- js 防抖 debounce 与 节流 throttle
debounce(防抖) 与 throttle(节流) 主要是用于用户交互处理过程中的性能优化.都是为了避免在短时间内重复触发(比如scrollTop等导致的回流.http请求等)导致的资源浪费问题. ...
- js 函数的防抖(debounce)与节流(throttle)
原文:函数防抖和节流: 序言: 我们在平时开发的时候,会有很多场景会频繁触发事件,比如说搜索框实时发请求,onmousemove, resize, onscroll等等,有些时候,我们并不能或者不想频 ...
- Java版的防抖(debounce)和节流(throttle)
概念 防抖(debounce) 当持续触发事件时,一定时间段内没有再触发事件,事件处理函数才会执行一次,如果设定时间到来之前,又触发了事件,就重新开始延时. 防抖,即如果短时间内大量触发同一事件,都会 ...
- js 函数的防抖(debounce)与节流(throttle) 带 插件完整解析版 [helpers.js]
前言: 本人纯小白一个,有很多地方理解的没有各位大牛那么透彻,如有错误,请各位大牛指出斧正!小弟感激不尽. 函数防抖与节流是做什么的?下面进行通俗的讲解. 本文借鉴:h ...
- 防抖(Debounce)与节流( throttle)区别
http://www.cnblogs.com/ShadowLoki/p/3712048.html http://blog.csdn.net/tina_ttl/article/details/51830 ...
- JavaScript 防抖(debounce)和节流(throttle)
防抖函数 触发高频事件后,n秒内函数只会执行一次,如果n秒内高频事件再次被触发,则重新计算时间 /** * * @param {*} fn :callback function * @param {* ...
- js 函数节流throttle 函数去抖debounce
1.函数节流throttle 通俗解释: 假设你正在乘电梯上楼,当电梯门关闭之前发现有人也要乘电梯,礼貌起见,你会按下开门开关,然后等他进电梯: 但是,你是个没耐心的人,你最多只会等待电梯停留一分钟: ...
- [JavaScript] 函数节流(throttle)和函数防抖(debounce)
js 的函数节流(throttle)和函数防抖(debounce)概述 函数防抖(debounce) 一个事件频繁触发,但是我们不想让他触发的这么频繁,于是我们就设置一个定时器让这个事件在 xxx 秒 ...
随机推荐
- session设置过期的方法(转载)
这篇文章主要介绍了php中实现精确设置session过期时间的方法,需要的朋友可以参考下 大多数据情况下我们对于session过期时间使用的是默认设置的时间,而对于一些有特殊要求的情况下我们可以设 ...
- poj2635The Embarrassed Cryptographer(同余膜定理)
The Embarrassed Cryptographer Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 15069 A ...
- JSP异常之org.apache.jasper.JasperException(转)
According to TLD or attribute directive in tag file, attribute items does not accep t any expression ...
- python jason,pickle
参考官网 https://docs.python.org/3/library/json.html https://docs.python.org/3/library/pickle.html 了解这两个 ...
- 【5】namenode启动过程
1.格式化空间(第一次启动的操作): 命令:bin/hadoop -format //用于格式化HDFS,如果不是首次格式化,需要删除下面配置的tmp目录后再进行core-site.xml的配置: / ...
- hibernate flushMode 错误
1 十一月 15, 2017 10:13:36 上午 org.apache.struts2.dispatcher.Dispatcher error 2 严重: Exception occurred d ...
- C# 判断文件是否文本文件
在网上查了好多资料,大部分都是通过将文件读成二进制流,取前两个字节判断,比如.jpg的是255216.代码如下: ); i++; }return isTextFile; }catch (Excepti ...
- Windows Forms DataGridView中合并单元格
Windows Forms DataGridView 没有提供合并单元格的功能,要实现合并单元格的功能就要在CellPainting事件中使用Graphics.DrawLine和 Graphics.D ...
- Xshell 的安装教程
Xshell就是一个远程控制RHEL的软件:其他的还有很多,用什么都无所谓(根据公司情况). 下面我们来安装下这个工具: 双击exe 点下一步: 选 免费的 然后下一步:(免费的功能足够用了) 点接受 ...
- 基于 HTML5 WebGL 的 3D “弹力”布局
分子力(molecular force),又称分子间作用力.范得瓦耳斯力,是指分子间的相互作用.当二分子相距较远时,主要表现为吸引力,这种力主要来源于一个分子被另一个分子随时间迅速变化的电偶极矩所极化 ...