防抖与节流

防抖与节流是不一样的,防抖是多次执行变成最后一次执行,节流是将多次执行变成间隔一段时间执行.

防抖:是当事件被触发后,延迟n秒后再执行回调,如果在这n秒内,事件有被触发,则重新计时                常见的场景如:搜索框   

步骤:1.获取页面中input DOM元素并监听input每次输入的内容     

        2.封装一个防抖函数,定义一个防抖的timer延迟器并置为空,先清除延时器,确保连续多次触发               但只执行最后一次定时器

        3. 模拟请求数据         

<body>
		<h2>js防抖</h2>
		<input type="text" placeholder="请输入内容" />
	</body>
	<script>
	//防抖与节流是不一样的,防抖是多次执行变成最后一次执行
	                    //节流是将多次执行,变成每隔一段时间执行 
							  
	// 获取inp标签
	let inp=document.querySelector('input');
	addEventListener('input',debounce(demo,2000))
	
	
	// 防抖封装
	function debounce(fn,wait){
		let timer=null;
		return function(){
			if(timer) clearTimeout(timer) //先清空定时器
			timer =setTimeout(fn,wait)
		}
	}
	// 每次输入内容都在清除定时器和创建定时器,直到你不在输入的时候就等2000毫秒之后进行执行函数,请求
	
	//模拟请求
	function demo(){
		console.log('发起请求')
	}
	</script>

   

节流是:throttle() 可以减少一段时间内事件的触发频率;  可以通过两种方式来写节流 一种是通过事件戳,另一种是通过定时器,如何判断什么时候用时间戳,什么时候用定时器呢? 

利用时间戳:如果需求就是第一次触发事件就需要执行一次回调函数

如下代码:

<body>
	<h2>js节流</h2>
	<div class="box"></div>
</body>
<script>
 
    
    let box=document.querySelector('div');//获取标签DOM
	box.addEventListener('mousemove',throttle(demo,1500))
    监听鼠标移动事件,设置节流

    //事件被频繁触发时,利用事件戳,设置一个过去的时间,和返回一个现在的时间,
    //当现在的时间-去已过去的时间大于你设置的时间,就执行函数请求,但这种有一个

   // 通过(事件戳)来写节流第一次触发时通过程序执行的时间,而不是事件第一次触发的起始时间
	function throttle(fn,time){
		let  record=Date.now();
		
		return function(){
			let   now=Date.now();//程序为起始时间
			let  interval=now-record;
			if(interval>time){
				fn();
				record = now;
			} 
		}
	}

function demo(){
	console.log('请求数据')
}
</script>
<style>
.box{
	width:200px;
	height:200px;
	background:cyan;
}
</style>

利用定时器:如果需求是一用户第一次触发事件为起始时间进行节流

<body>
	<h2>js节流</h2>
	<div class="box"></div>
</body>
<script>
    let box=document.querySelector('div');//获取标签DOM
	box.addEventListener('mousemove',throttle(demo,1500))
    //监听鼠标移动事件,设置节流


   function  throttle(fn,time){
		let done=false; //初始化没有定时器 ,事件触发的时间为起始时间
		return function(){
			if(!done){
				setTimeout(()=>{
					fn();
					done=false; //设置为false,在下一个周期的时候没有定时器
				},time)
				done=true;//有定时器了设置为true
			}
		}
	}
	function demo(){
		console.log('请求数据')
	}
</script>
<style>
.box{
	width:200px;
	height:200px;
	background:cyan;
}
</style>

这就是防抖与节流的一些小案例!!!                                                                                          

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
在Vue中,有一些常用的防抖节流插件可以方便地实现防抖节流的功能。以下是两个常用的插件: 1. Lodash(防抖节流) Lodash是一个JavaScript实用工具库,它提供了许多常用的函数和方法,包括防抖节流函数。使用Lodash的`debounce`和`throttle`函数可以很方便地实现防抖节流。 安装Lodash: ```bash npm install lodash ``` 使用示例: ```javascript import { debounce, throttle } from 'lodash'; // 防抖示例 const debouncedFunc = debounce(() => { console.log('执行操作'); }, 500); // 节流示例 const throttledFunc = throttle(() => { console.log('执行操作'); }, 200); ``` 2. Vue-lodash(防抖节流) Vue-lodash是一个专门为Vue开发的Lodash插件,它提供了Vue指令的方式来使用Lodash的方法,包括防抖节流。 安装Vue-lodash: ```bash npm install vue-lodash ``` 在Vue项目中使用Vue-lodash示例: ```javascript import Vue from 'vue'; import VueLodash from 'vue-lodash'; import { debounce, throttle } from 'lodash'; Vue.use(VueLodash, { lodash: { debounce, throttle } }); ``` 在Vue组件中使用防抖节流: ```html <template> <div> <button v-debounce:click="debouncedFunc">点击按钮(防抖)</button> <button v-throttle:click="throttledFunc">点击按钮(节流)</button> </div> </template> <script> export default { methods: { debouncedFunc() { console.log('执行操作'); }, throttledFunc() { console.log('执行操作'); }, }, }; </script> ``` 以上是两个常用的Vue插件,可以方便地在Vue项目中使用防抖节流功能。根据具体需求选择合适的插件来使用。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值