防抖和节流简单实现

本文详细介绍了JavaScript中两种重要的性能优化技术——函数节流(throttle)和防抖(debounce)的实现方式,分别利用Date对象和setTimeout进行实现。函数节流限制了函数的执行频率,而防抖则确保在频繁触发事件时,只执行最后一次。这两种技术在处理如窗口滚动、输入验证等场景中非常有用,能够有效提升网页性能。
摘要由CSDN通过智能技术生成
//利用date对象
function throttle (func,delay) {
	 let pre = 0
	 return function () {
	 		let now = new Date()
	 		let _this = this
	 		let args = arguments
	 		if(now -pre >delay){
				func.apply(_this,args)
				pre = now
            }
     }
}
//利用setTimeout
function throttle (func,delay) {
	let timer;
	return function (){
		let _this = this
		let args = arguments
		timer = setTimeout(() => {
       		func.apply(_this,args)
       		timer = null
        },delay)
   }
}


function debounce(fn,delay,immediate = false) {
		let timer = null
    let isInvoke = false
    const _debounce = function(...args) {
      if(timer) clearTimeout(timer)
      if(immediate && !isInvoke) {
           fn.apply(this,..args)
           isInvoke = true
      }else {
        timer = setTimeout(() =>{
             fn.apply(this,args)
             isInvoke = false
        },delay)
      }
    }
    return _debounce
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
JS中的防抖函数和节流函数是常用的函数优化方法,可以有效控制函数的执行频率和执行时间间隔。 防抖函数的实现可以使用闭包和定时器来实现。通过在函数执行前先清除之前的定时器,然后设置一个新的定时器来延迟函数的执行。如果在指定的时间内再次触发函数,则清除之前的定时器重新设置新的定时器,以此来达到延迟函数执行的效果。下面是一个防抖函数的示例代码: ```javascript function debounce(fn, delay) { var timer = null; return function () { if (timer) clearTimeout(timer); timer = setTimeout(function () { fn(); }, delay); } } ``` 节流函数的实现可以使用定时器和时间戳来实现。在函数的执行过程中,通过判断当前的时间戳与上次执行的时间戳的差值来控制函数的执行频率。如果距离上次执行的时间超过指定的时间间隔,则执行函数,并更新上次执行的时间戳。下面是一个节流函数的示例代码: ```javascript function throttle(fn, delay) { var lastTime = 0; return function () { var currentTime = Date.now(); if (currentTime - lastTime >= delay) { fn(); lastTime = currentTime; } } } ``` 防抖函数适用于在连续触发事件时,只在最后一次触发事件后执行一次函数,常用于输入框的输入事件等场景。节流函数适用于在连续触发事件时,按照指定的时间间隔执行函数,常用于滚动事件、窗口大小改变事件等场景。 以上是防抖函数和节流函数的简单实现和应用场景的介绍,希望对您有所帮助。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [JavaScript 防抖节流实现](https://blog.csdn.net/weixin_43853746/article/details/122654312)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* [JS函数节流防抖之间的区分和实现详解](https://download.csdn.net/download/weixin_38622962/12947757)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值