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

debounce与throttle的区别主要在于:

1. debounce是通过设置定时器,在延迟的时间内每次触发都会重置定时器,直到在某个延迟时间点内不再触发事件才会执行。

2. throttle也是通过设置定时器,只是在延迟时间内用户只有首次触发是有效的,其他触发都是无效的,只有等延迟时间到了才会执行该事件。

理论有点枯燥,直接看代码:

 <!DOCTYPE html>
<html>
<head>
<title>debounce</title>
<style type="text/css">
#box {
width: 300px;
height: 150px;
border: 1px solid #eee;
background-color: #e3e3e3;
}
</style>
</head>
<body>
<div id="box"></div> <script type="text/javascript">
let divEle = document.querySelector('#box');
let count = 0;
divEle.addEventListener('mousemove', debounce(cbFn, 1000)); function debounce(callback, delay) {
let timer = null;
return function() {
timer && clearTimeout(timer);
timer = setTimeout(function(){
callback.apply();
}, delay)
}
} function cbFn() {
divEle.innerHTML = ++count;
}
</script>
</body>
</html>
 <!DOCTYPE html>
<html>
<head>
<title>throttle</title>
<style type="text/css">
#box {
width: 300px;
height: 150px;
border: 1px solid #eee;
background-color: #e3e3e3;
}
</style>
</head>
<body>
<div id="box"></div> <script type="text/javascript">
let divEle = document.querySelector('#box');
let count = 0;
divEle.addEventListener('mousemove', throttle(cbFn, 1000)); function throttle(callback, delay) {
let preTime = new Date().getTime();
return function() {
const now = new Date().getTime();
if (now - preTime > delay) {
preTime = now;
setTimeout(function() {
cbFn();
}, delay)
}
}
} function cbFn() {
divEle.innerHTML = ++count;
}
</script>
</body>
</html>

js 防抖 debounce 与 节流 throttle的更多相关文章

  1. 防抖debounce和节流throttle

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

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

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

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

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

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

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

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

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

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

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

  7. C#.Net下的防抖-Debounce和节流阀-Throttle功能实现

    C#下的防抖-Debounce.节流阀-Throttle功能实现 防抖-Debounce 连续的多次调用,只有在调用停止之后的一段时间内不再调用,然后才执行一次处理过程. 节流阀-Throttle 连 ...

  8. JS中的函数节流throttle详解和优化

    JS中的函数节流throttle详解和优化在前端开发中,有时会为页面绑定resize事件,或者为一个页面元素绑定拖拽事件(mousemove),这种事件有一个特点,在一个正常的操作中,有可能在一个短的 ...

  9. JS 防抖函数和节流函数

    文章转载自:木上有水 什么是防抖?什么是节流? 工作中我们经常会用一些方法监听某些事件的完成,比如scroll.resize.keyup等. 常规事件触发的时候,比如scroll,会在短时间内触发多次 ...

随机推荐

  1. iframe父页面和子页面获取元素和js变量

    父页面获取iframe页面元素和变量 获取方法:$("#id")[0].contentWindow.showInfo(): 获取元素:  $("#id").co ...

  2. angular配置路由/子页面+vue配置路由/子页面

    1.在vue.js中组件可以复用,然后最近配置了几个子页面 在 这个文件中配置路由,子页面的配置跟其他一样,只不过path不同.   routes: [     { path: '/',       ...

  3. mysql 查询正在执行的进程-亲试ok

    命令:show processlist 每一列的含义和用途: 第一列 id,不用说了吧,一个标识,你要kill一个语句的时候很有用. 第二列 user列,显示单前用户,如果不是root,这个命令就只显 ...

  4. index.html jquery

    index.html   <!DOCTYPE html> <html lang="en"> <head> <meta charset=&q ...

  5. 本地计算机上的 postgresql 服务启动后停止解决方法

    在启动 postgresql 服务是遇到这种情况: 解决方法: 打开计算机管理====>查看应用程序日志信息,可以看出,由于日志配置错误的问题. 找到 postgresql.conf 文件,做如 ...

  6. .net core 发送邮件

    var message = new MimeMessage();            //发送方            message.From.Add(new MailboxAddress(&qu ...

  7. jquery <div> 排序

    var asc_active = function(a, b) { var av = Number($(a).find('.active_num .v').html()); var bv = Numb ...

  8. location.href 本窗口与window.open 新窗口打开用法

    二种新窗口打开的区别: window.open("URL",'top'); 只是表示打开这个页面,并不是打开并刷新页面: window.location.href="UR ...

  9. [Err] 1093 - You can't specify target table 'master_data' for update in FROM clause

    delete from master_data where category_id not in (select category_id from master_data a, bc_category ...

  10. 小白的python之路10/29 文件归档

    一打包解包文件 [root@localhost ~]# cd /test/[root@localhost test]# touch a.txt b.txt c.txt[root@localhost t ...