一分钟带你搞定前端”防抖节流“
创始人
2024-04-14 20:35:03
0

序:

项目中必不可少都会用到防抖和节流,于是今天就总结下常规用法。

一、lodash.js防抖节流

1.下载lodash

# Yarn
$ yarn add lodash
$ yarn add lodash-es
# NPM
$ npm install lodash --save
$ npm install lodash-es --save

2.引入lodash

//引入到 main.js 文件中
import lodash from "lodash";
import { debounce } from "lodash-es";// 将全局lodash对象指向给Vue作用域下
app.config.globalProperties.$lodash = lodash; 

3.防抖用法

debounce API走起

_.debounce(func, [wait=0], [options={}])

func (Function): 要防抖动的函数。

[wait=0] (number): 需要延迟的毫秒数。

[options={}] (Object): 选项对象。

[options.leading=false] (boolean): 指定在延迟开始前调用,默认false。

[options.maxWait] (number): 设置 func 允许被延迟的最大值。

[options.trailing=true] (boolean): 指定在延迟

结束后调用,默认true。

testDebounce: _.debounce(function() {console.log("debounce");
}, 2000, {leading: true,trailing: false
})testDebounce: debounce(function() {console.log("debounce");
}, 2000)

4.节流用法

throttle API走起

_.throttle(func, [wait=0], [options={}]) func (Function): 要节流的函数。

[wait=0] (number): 需要节流的毫秒数。

[options={}] (Object): 选项对象。

[options.leading=true] (boolean): 指定调用在节流开始前,默认true。

[options.trailing=true] (boolean): 指定调用在节流结束后,默认true。

testThrottle: _.throttle(function() {console.log("throttle");
}, 5000, {leading: true,trailing: false
})

5.vue内对这两种防抖截流的使用方法

 

 


6.react中使用

test = () => {console.log('--------------11111---------------');this.fun();//方法调用
}//debounce使用
fun = _.debounce(function(e){console.log('--------------22222---------------');
}, 5000,{leading: true,trailing: false,
})//throttle使用
fun = _.throttle(function(e){console.log('--------------22222---------------');
}, 5000,{leading: true,trailing: false,
})

二、手写防抖节流

1.防抖事件

//就是在你的事件前面加上vm.$lodash.debounce((),500),后面接上多久执行一次const addSubtaskClick = vm.$lodash.debounce(() => {if (inputData.value === '' || inputData.value === null) {return messageInfo.error('请输入内容!')} else {let childTaskList = [{title: inputData.value,},]editTasks({ childTaskList: childTaskList })inputData.value = ''footerInput.value = falseemit('modificationsClicl', code)// addSubtasks()}if (MyData.childTaskList.length === 0) {showSonMy.value = false}footerInput.value = falseinputData.value = null}, 500)

2.写节流事件

//就是在你的事件前面加上vm.$lodash.debounce((),500)const handerClick = vm.$lodash.throttle((item, index) => {\activeCheck.value = index;\localStorage.setItem('teamMessage', JSON.stringify(item));\store.commit('common/teamMessageChange', JSON.stringify(item));\router.push('/myTask');\}, 500);

相关内容

热门资讯

监控摄像头接入GB28181平... 流程简介将监控摄像头的视频在网站和APP中直播,要解决的几个问题是:1&...
Windows10添加群晖磁盘... 在使用群晖NAS时,我们需要通过本地映射的方式把NAS映射成本地的一块磁盘使用。 通过...
protocol buffer... 目录 目录 什么是protocol buffer 1.protobuf 1.1安装  1.2使用...
在Word、WPS中插入AxM... 引言 我最近需要写一些文章,在排版时发现AxMath插入的公式竟然会导致行间距异常&#...
【PdgCntEditor】解... 一、问题背景 大部分的图书对应的PDF,目录中的页码并非PDF中直接索引的页码...
Fluent中创建监测点 1 概述某些仿真问题,需要创建监测点,用于获取空间定点的数据࿰...
educoder数据结构与算法...                                                   ...
MySQL下载和安装(Wind... 前言:刚换了一台电脑,里面所有东西都需要重新配置,习惯了所...
MFC文件操作  MFC提供了一个文件操作的基类CFile,这个类提供了一个没有缓存的二进制格式的磁盘...
有效的括号 一、题目 给定一个只包括 '(',')','{','}'...