前言

故事发生在与大创的又一次撕逼(日常)中,我方坚定的认为:作为社会主义接班人,节流与消抖的界限是明显的,是不容混肴的,是不可侵犯的!对方辩友坚持地觉得:界限是模糊的,行为是暧昧的,性别是可以忽视的(。。。)。本着凡事要往祖坟上刨的精神,对这两个概念进行了一番深刻的社会主义改造。

定义

节流(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)的更多相关文章

  1. 防抖debounce和节流throttle

    大纲 一.出现缘由 二.什么是防抖debounce和节流throttle 三.应用场景 3.1防抖 3.2节流 一.出现缘由 前端开发中,有一部分用户行为会频繁触发事件,而对于DOM操作,资源加载等耗 ...

  2. js 防抖 debounce 与 节流 throttle

    debounce(防抖) 与 throttle(节流) 主要是用于用户交互处理过程中的性能优化.都是为了避免在短时间内重复触发(比如scrollTop等导致的回流.http请求等)导致的资源浪费问题. ...

  3. js 函数的防抖(debounce)与节流(throttle)

    原文:函数防抖和节流: 序言: 我们在平时开发的时候,会有很多场景会频繁触发事件,比如说搜索框实时发请求,onmousemove, resize, onscroll等等,有些时候,我们并不能或者不想频 ...

  4. Java版的防抖(debounce)和节流(throttle)

    概念 防抖(debounce) 当持续触发事件时,一定时间段内没有再触发事件,事件处理函数才会执行一次,如果设定时间到来之前,又触发了事件,就重新开始延时. 防抖,即如果短时间内大量触发同一事件,都会 ...

  5. js 函数的防抖(debounce)与节流(throttle) 带 插件完整解析版 [helpers.js]

    前言:         本人纯小白一个,有很多地方理解的没有各位大牛那么透彻,如有错误,请各位大牛指出斧正!小弟感激不尽.         函数防抖与节流是做什么的?下面进行通俗的讲解. 本文借鉴:h ...

  6. 防抖(Debounce)与节流( throttle)区别

    http://www.cnblogs.com/ShadowLoki/p/3712048.html http://blog.csdn.net/tina_ttl/article/details/51830 ...

  7. JavaScript 防抖(debounce)和节流(throttle)

    防抖函数 触发高频事件后,n秒内函数只会执行一次,如果n秒内高频事件再次被触发,则重新计算时间 /** * * @param {*} fn :callback function * @param {* ...

  8. js 函数节流throttle 函数去抖debounce

    1.函数节流throttle 通俗解释: 假设你正在乘电梯上楼,当电梯门关闭之前发现有人也要乘电梯,礼貌起见,你会按下开门开关,然后等他进电梯: 但是,你是个没耐心的人,你最多只会等待电梯停留一分钟: ...

  9. [JavaScript] 函数节流(throttle)和函数防抖(debounce)

    js 的函数节流(throttle)和函数防抖(debounce)概述 函数防抖(debounce) 一个事件频繁触发,但是我们不想让他触发的这么频繁,于是我们就设置一个定时器让这个事件在 xxx 秒 ...

随机推荐

  1. Toxophily

    Problem Description The recreation center of WHU ACM Team has indoor billiards, Ping Pang, chess and ...

  2. IdentityServer4 配置负载均衡

    如果使用 IdentityServer4 做授权服务的负载均衡,默认情况下是不可以的,比如有两个授权服务站点,一个资源服务绑定其中一个授权服务(Authority配置),如果通过另外一个授权服务获取a ...

  3. 在centos上安装jenkins

    摘要: 本篇介绍了如何在linux服务器上安装jenkins 一:使用war安装 官网地址:https://jenkins.io/doc/ Guided Tour This guided tour w ...

  4. sass学习--安装ruby

    1.下载ruby:https://rubyinstaller.org/downloads/ 2.安装完ruby之后,在开始菜单中,找到刚才我们安装的ruby,打开Start Command Promp ...

  5. JAVASE高级2

    反射概述 什么是反射? 反射的的概念是有smith1982年首次提出的,zhuy主要是指程序可以访问.检测和修改它本身状态或行为的一种能力. JAVA反射机制是运行状态中,对于任意一个类,都能够知道这 ...

  6. Android-Async-Http 特性简单分析

    如下是官方文档描述此库的特点: All requests are made outside of your app’s main UI thread, but any callback logic w ...

  7. 实现验证码图像文字的识别(C#调用DLL)

    请先下载http://asprise.com/product/ocr/index.php?lang=csharp 的SDK.里面提供了详细的OCR方法,如下:   将发现图像框picbVeryfyCo ...

  8. 引用reference作用域scope闭包closure上下文context用法

    引用(reference).作用域(scope).闭包(closure)以及上下文(context)是JavaScript重中之重的基础,也是学习好JavaScript的基础.在这里我以浅显的理解给大 ...

  9. SHA安全散列算法简析

    1 SHA算法简介 1.1 概述 SHA (Secure Hash Algorithm,译作安全散列算法) 是美国国家安全局 (NSA) 设计,美国国家标准与技术研究院(NIST) 发布的一系列密码散 ...

  10. 简单的OC程序

    知识点 1.#import的用途: 1> 跟#include一样,拷贝文件的内容 2> 可以自动防止文件的内容被重复拷贝 2.#import <Foundation/NSObjCRu ...