js debounce & throttle All In One
js debounce & throttle All In One
debounce & throttle
js 节流 防抖
debounce 防抖
防抖,是指一个事件触发后在单位时间内,如果发生重复触发了同一事件,则取消上一次的事件,并重新计时️
应用场景:实时搜索框,等待用户输入完成,才发送 ajax 请求;减少不必要的请求次数,提高性能
demo
https://codepen.io/xgqfrms/pen/rNNoroy
// debounce
// 防抖,是指一个事件触发后在单位时间内,如果发生重复触发了同一事件,则取消上一次的事件,并重新计时️
// 应用场景:实时搜索框,等待用户输入完成,才发送 ajax 请求;减少不必要的请求次数,提高性能
const debounce = (func, delay) => {
return function(args) {
const that = this;
func.id && clearTimeout(func.id);
func.id = setTimeout(() => {
func.call(that, args);
}, delay);
};
};
const ajax = e => {
const value = e.target.value || ``;
console.log(`ajax value`, value);
pre.insertAdjacentHTML(`beforeend`, `${value} \n`);
};
const input = document.querySelector(`[data-uid="input"]`);
const inputDebounce = document.querySelector(`[data-uid="inputDebounce"]`);
const pre = document.querySelector(`[data-uid="pre"]`);
const btn = document.querySelector(`[data-uid="btn"]`);
btn.addEventListener(`click`, () => {
input.value = ``;
inputDebounce.value = ``;
pre.innerHTML = ``;
});
input.addEventListener(`input`, ajax);
inputDebounce.addEventListener(`input`, debounce(ajax, 500));
throttle 节流
节流,是指在单位时间内, 只会触发一次事件,如果事件触发后,又重复触发了同一事件,则忽略后面触发的事件,直到第一次事件的计时️结束
应用场景:埋点 ??? 监听滚动事件,比如是否滑到底部自动加载更多,用throttle来判断
demo
https://codepen.io/xgqfrms/pen/yLLGxZv
// throttle
// 节流,是指在单位时间内, 只会触发一次事件,如果事件触发后,又重复触发了同一事件,则忽略后面触发的事件,直到第一次事件的计时️结束
// 应用场景:埋点???
const throttle = (func, delay) => {
let flag = true;
return function(args) {
const that = this;
if(flag) {
flag = false;
func.id = setTimeout(() => {
func.call(that, args);
clearTimeout(func.id);
flag = true;
}, delay);
} else {
// ignore
console.log(`ignore event`);
}
};
};
const ajax = e => {
const value = e.target.value || ``;
console.log(`ajax value`, value);
pre.insertAdjacentHTML(`beforeend`, `${value} \n`);
};
const input = document.querySelector(`[data-uid="input"]`);
const inputThrottle = document.querySelector(`[data-uid="inputThrottle"]`);
const pre = document.querySelector(`[data-uid="pre"]`);
const btn = document.querySelector(`[data-uid="btn"]`);
btn.addEventListener(`click`, () => {
input.value = ``;
inputThrottle.value = ``;
pre.innerHTML = ``;
});
input.addEventListener(`input`, ajax);
inputThrottle.addEventListener(`input`, throttle(ajax, 3000));
debounce / throttling
防抖 / 节流
https://www.npmjs.com/package/throttle-debounce
前端性能优化
白屏/首屏 性能优化
https://www.cnblogs.com/xgqfrms/p/13654839.html#4681503
lodash
https://lodash.com/docs/4.17.15#debounce
https://lodash.com/docs/4.17.15#throttle
_.debounce(func, [wait=0], [options={}])
_.throttle(func, [wait=0], [options={}])
lodash 源码剖析
https://github.com/xgqfrms/lodash/issues/1
underscore 源码剖析
https://github.com/xgqfrms/underscore/issues/1
refs
https://css-tricks.com/the-difference-between-throttling-and-debouncing/
https://css-tricks.com/debouncing-throttling-explained-examples/
https://codeburst.io/throttling-and-debouncing-in-javascript-b01cad5c8edf
https://www.telerik.com/blogs/debouncing-and-throttling-in-javascript
https://github.com/niksy/throttle-debounce#readme
https://blog.bitsrc.io/understanding-throttling-and-debouncing-973131c1ba07
xgqfrms 2012-2020
www.cnblogs.com 发布文章使用:只允许注册用户才可以访问!
js debounce & throttle All In One的更多相关文章
- 关于Js debounce 函数小结
一.前言 以下场景往往由于事件频繁被触发,因而频繁执行DOM操作.资源加载等重行为,导致UI停顿甚至浏览器崩溃. 1. window对象的resize.scroll事件 2. 拖拽时的mousemov ...
- 浅谈 Unserscore.js 中 _.throttle 和 _.debounce 的差异
来源:http://blog.coding.net/blog/the-difference-between-throttle-and-debounce-in-underscorejs Unsersco ...
- [JavaScript] 函数节流(throttle)和函数防抖(debounce)
js 的函数节流(throttle)和函数防抖(debounce)概述 函数防抖(debounce) 一个事件频繁触发,但是我们不想让他触发的这么频繁,于是我们就设置一个定时器让这个事件在 xxx 秒 ...
- 详解防抖函数(debounce)和节流函数(throttle)
本文转自:https://www.jianshu.com/p/f9f6b637fd6c 闭包的典型应用就是函数防抖和节流,本文详细介绍函数防抖和节流的应用场景和实现. 函数防抖(debounce) 函 ...
- 白话debounce和throttle
遇到的问题 在开发过程中会遇到频率很高的事件或者连续的事件,如果不进行性能的优化,就可能会出现页面卡顿的现象,比如: 鼠标事件:mousemove(拖曳)/mouseover(划过)/mouseWhe ...
- -_-#【Better Code】throttle / debounce
浅谈javascript的函数节流 javascript函数的throttle和debounce throttle 疯狂触发事件,固定步调执行 debounce 疯狂触发事件,不会执行 var res ...
- lodash throttle和debounce
https://lodash.com/docs#debounce throttle(又称节流)和debounce(又称防抖)其实都是函数调用频率的控制器 throttle:将一个函数的调用频率限制在一 ...
- throttle和debounce
遇到的问题 在开发过程中会遇到频率很高的事件或者连续的事件,如果不进行性能的优化,就可能会出现页面卡顿的现象,比如: 鼠标事件:mousemove(拖曳)/mouseover(划过)/mouseWhe ...
- js函数防抖、节流实现
防抖 Debounce 函数防抖就是,延迟一段时间再执行函数,如果这段时间内又触发了该函数,则延迟重新计算: // 简单实现 function debounce(fn, wait) { let t r ...
随机推荐
- Windows 2008server部署pxe启动安装windows系统
前期准备: 需安装的角色有:AD域-DHCP服务器-DNS服务器-Windows部署服务,我是将这几个服务都安装在一台vps上,C盘50G,D盘100G 安装好后角色会列出所安装的服务,如下图: 1. ...
- ELK (elasticsearch+kibana+logstash+elasticsearch-head) 华为云下载地址
https://mirrors.huaweicloud.com/elasticsearch https://mirrors.huaweicloud.com/kibana https://mirrors ...
- java.io.IOException: Could not find resource com/xxx/xxxMapper.xml
java.io.IOException: Could not find resource com/xxx/xxxMapper.xml 报错内容: org.apache.ibatis.exception ...
- axios用法
1 axios.get('https://api.apiopen.top/getJoke?type=all', { 2 params: {//用于传参 3 type: 'all' 4 } 5 }).t ...
- https://channels.readthedocs.io/en/latest/tutorial/part_2.htmlhttps://channels.readthedocs.io/en/latest/tutorial/part_2.html
https://channels.readthedocs.io/en/latest/tutorial/part_2.html
- 深入理解SPI机制-服务发现机制
https://www.jianshu.com/p/3a3edbcd8f24 SPI ,全称为 Service Provider Interface,是一种服务发现机制.它通过在ClassPath路径 ...
- java 本地方法(JNI)
最近搞了一个调用第三方so库做登录认证的任务,以前对JNI没什么概念,最近学习了 <java核心技术> 本地方法 一章,把自己写的一些例子记录一下. 自己C语言真是渣渣,所以所有的例子都在 ...
- 数据湖-Apache Hudi
Hudi特性 数据湖处理非结构化数据.日志数据.结构化数据 支持较快upsert/delete, 可插入索引 Table Schema 小文件管理Compaction ACID语义保证,多版本保证 并 ...
- 玩转IDEA项目结构Project Structure,打Jar包、模块/依赖管理全搞定
前言 你好,我是A哥(YourBatman). 如何给Module模块单独增加依赖? 如何知道哪些Module模块用了Spring框架,哪些是web工程? IDEA如何打Jar包?打War包? 熟练的 ...
- Kubernetes --(k8s)volume 数据管理
容器的磁盘的生命周期是短暂的,这就带来了许多问题:第一:当一个容器损坏了,kubelet会重启这个容器,但是数据会随着container的死亡而丢失:第二:当很多容器在同一Pod中运行的时候,经常需要 ...